0
votes

I am new in tensorflow, i am trying to train to tensorflow models which are connected with a dot product output layer. The input are two 2048 float vectors.

When I run the script I always get these errors:

Traceback (most recent call last):

File "classifier.py", line 120, in _, summary = sess.run([optimizer, merged], feed_dict={x1: batch_x1s, x2: batch_x2s})

File "/Users/Joachim/work/tensorflow/virtualenv/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 789, in run run_metadata_ptr) File "/Users/Joachim/work/tensorflow/virtualenv/tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 968, in _run np_val = np.asarray(subfeed_val, dtype=subfeed_dtype) File "/Users/Joachim/work/tensorflow/virtualenv/tensorflow/lib/python3.6/site-packages/numpy/core/numeric.py", line 531, in asarray return array(a, dtype, copy=False, order=order)

ValueError: setting an array element with a sequence.

Here is my code:

import tensorflow as tf
import sys
import math
import os
import numpy as np
import json
import argparse

from sklearn.model_selection import train_test_split
from tqdm import tqdm
from tensorflow.python.platform import gfile
from progress.bar import Bar


bottleneck_dir = 'bottlenecks'


### LOAD DATA FROM BOTTLENECKS
data_inputs = []
data_labels = []
data_expected_result=[]

bottleneck_list = []
file_glob = os.path.join(bottleneck_dir, '*.txt')
bottleneck_list.extend(gfile.Glob(file_glob))

for bottleneck_file in bottleneck_list:
    bottleneck = open(bottleneck_file)  
    bottleneck_string = bottleneck.read()
    bottleneck_values = [float(x) for x in bottleneck_string.split(',')]
    imageName=bottleneck_file.split('.')[0]
    helper=False
    for i in range(len(data_labels)): 
        if imageName==data_labels[i]:
            if 'search' in bottleneck_file:
                data_inputs[i][0]=bottleneck_values
            else:
                data_inputs[i][1]=bottleneck_values
            helper=true
    if helper!=True:
        if 'search' in bottleneck_file:
            data_inputs.append([bottleneck_values,[]])
        else:
            data_inputs.append([[],bottleneck_values])
        data_expected_result.append(1);

data_inputs_x1 = [i[0] for i in data_inputs]
data_inputs_x2 = [i[1] for i in data_inputs]



# Setting hyperparameters
learning_rate = 0.01
batch_size = 4
epochs = 1
log_batch_step = 50


n_features = np.size(data_inputs, 1)

tf.reset_default_graph()
graph = tf.get_default_graph()


inputVectorSize=2048
outputVectorSize=2048

x1 = tf.placeholder(tf.float32, [None, inputVectorSize], name='x1')#input layer

x2 = tf.placeholder(tf.float32, [None, inputVectorSize], name='x2')#input layer

dense1 = tf.layers.dense(inputs=x1, units=inputVectorSize, activation=tf.nn.relu)
logits1 = tf.layers.dense(inputs=dense1, units=outputVectorSize, activation=tf.nn.relu)
logits1_normalized=tf.nn.softmax(logits1)
dense2 = tf.layers.dense(inputs=x2, units=inputVectorSize, activation=tf.nn.relu)
logits2 = tf.layers.dense(inputs=dense2, units=outputVectorSize, activation=tf.nn.relu)
logits2_normalized=tf.nn.softmax(logits2)
output = tf.reduce_sum( tf.multiply( logits1_normalized, logits2_normalized), 1, keep_dims=True )


# Defining loss of network
loss = data_expected_result-output
tf.summary.scalar('loss', loss)

# Setting optimiser
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)

# Define accuracy
accuracy = loss
tf.summary.scalar('accuracy', accuracy)

# For saving checkpoint after training
saver = tf.train.Saver()

merged = tf.summary.merge_all()

# use in command line: tensorboard --logdir=path/to/log  --> to view tensorboard

# Run tensorflow session
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    train_writer = tf.summary.FileWriter('log', sess.graph)
    tf.train.write_graph(sess.graph_def, '', 'savedgraph.pbtxt', as_text=False)

    # Running the training in batches 
    batch_count = int(math.ceil(len(data_inputs)/batch_size))

    for epoch_i in range(epochs):
        batches_pbar = tqdm(range(batch_count), desc='Epoch {:>2}/{}'.format(epoch_i+1, epochs), unit='batches')
        # The training cycle
        for batch_i in batches_pbar:
            # Get a batch of training features and labels
            batch_start = batch_i*batch_size
            batch_x1s = data_inputs_x1[batch_start:batch_start + batch_size]
            batch_x2s = data_inputs_x2[batch_start:batch_start + batch_size]
            # Run optimizer
            _, summary = sess.run([optimizer, merged], feed_dict={x1: batch_x1s, x2: batch_x2s})
            train_writer.add_summary(summary, batch_i)

        # Check accuracy against validation data
        val_accuracy, val_loss = sess.run([accuracy, loss], feed_dict={x1: data_inputs_x1[0:len(data_inputs-1)], x2: data_inputs_x2[0:len(data_inputs-1)]})
        print("After epoch {}, Loss: {}, Accuracy: {}".format(epoch_i+1, val_loss, val_accuracy))


    test_accuracy, test_loss = sess.run([accuracy, loss], feed_dict={x1: data_inputs_x1[0:len(data_inputs-1)], x2: data_inputs_x2[0:len(data_inputs-1)]})
    print ("TEST LOSS: {}, TEST ACCURACY: {}".format(test_loss, test_accuracy))

    g = tf.get_default_graph()
    saver.save(sess, 'savedgraph')

Can anyone show my what to do to fix the problem?

2

2 Answers

0
votes

You need to feed array not list. change the lines where used list as feed_dict input.

batch_x1s = np.asarray(data_inputs_x1[batch_start:batch_start + batch_size])
batch_x2s = np.asarray(data_inputs_x2[batch_start:batch_start + batch_size])

...
test_accuracy, test_loss = sess.run([accuracy, loss], feed_dict=
     {x1:np.asarray(data_inputs_x1[0:len(data_inputs-1)]), x2:  
        np.asarray(data_inputs_x2[0:len(data_inputs-1)])})
0
votes

I found the Problem, it was a problem with the input data.

import tensorflow as tf
import sys
import math
import os
import numpy as np
import json
import argparse

from sklearn.model_selection import train_test_split
from tqdm import tqdm
from tensorflow.python.platform import gfile
from progress.bar import Bar


bottleneck_dir = 'bottlenecks'


### LOAD DATA FROM BOTTLENECKS
data_inputs = []
data_labels = []
data_expected_result=[]

bottleneck_list = []
file_glob = os.path.join(bottleneck_dir, '*.txt')
bottleneck_list.extend(gfile.Glob(file_glob))

for bottleneck_file in bottleneck_list:
    bottleneck = open(bottleneck_file)  
    bottleneck_string = bottleneck.read()
    bottleneck_values = [float(x) for x in bottleneck_string.split(',')]
    imageName=bottleneck_file.split('.')[0]
    helper=False
    for i in range(len(data_labels)): 
        if imageName==data_labels[i]:
            if 'search' in bottleneck_file:
                data_inputs[i][0]=np.asarray(bottleneck_values)
            else:
                data_inputs[i][1]=np.asarray(bottleneck_values)
            helper=True
    if helper!=True:
        if 'search' in bottleneck_file:
            data_inputs.append([bottleneck_values,[]])
        else:
            data_inputs.append([[],bottleneck_values])
        data_expected_result.append(1);
        data_labels.append(imageName);

data_inputs_x1 = [i[0] for i in data_inputs]
data_inputs_x2 = [i[1] for i in data_inputs]


for i in range(len(data_inputs_x2)): 
    print(len(data_inputs_x2[i]))

# Setting hyperparameters
learning_rate = 0.01
batch_size = 4
epochs = 1
log_batch_step = 50


n_features = np.size(data_inputs, 1)

tf.reset_default_graph()
graph = tf.get_default_graph()


inputVectorSize=2048
outputVectorSize=2048

x1 = tf.placeholder(tf.float32, [None, inputVectorSize], name='x1')#input layer

x2 = tf.placeholder(tf.float32, [None, inputVectorSize], name='x2')#input layer

dense1 = tf.layers.dense(inputs=x1, units=inputVectorSize, activation=tf.nn.relu)
logits1 = tf.layers.dense(inputs=dense1, units=outputVectorSize, activation=tf.nn.relu)
logits1_normalized=tf.nn.softmax(logits1)
dense2 = tf.layers.dense(inputs=x2, units=inputVectorSize, activation=tf.nn.relu)
logits2 = tf.layers.dense(inputs=dense2, units=outputVectorSize, activation=tf.nn.relu)
logits2_normalized=tf.nn.softmax(logits2)
output = tf.reduce_sum( tf.multiply( logits1_normalized, logits2_normalized), 1, keep_dims=True )


# Defining loss of network
loss = tf.reduce_sum(tf.subtract(1.0,output));
tf.summary.scalar('loss', loss)

# Setting optimiser
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)

# Define accuracy
accuracy = loss
tf.summary.scalar('accuracy', accuracy)

# For saving checkpoint after training
saver = tf.train.Saver()

merged = tf.summary.merge_all()

# use in command line: tensorboard --logdir=path/to/log  --> to view tensorboard

# Run tensorflow session
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    train_writer = tf.summary.FileWriter('log', sess.graph)
    tf.train.write_graph(sess.graph_def, '', 'savedgraph.pbtxt', as_text=False)

    # Running the training in batches 
    batch_count = int(math.ceil(len(data_inputs)/batch_size))

    for epoch_i in range(epochs):
        batches_pbar = tqdm(range(batch_count), desc='Epoch {:>2}/{}'.format(epoch_i+1, epochs), unit='batches')
        # The training cycle
        for batch_i in batches_pbar:
            # Get a batch of training features and labels
            batch_start = batch_i*batch_size
            batch_x1s = np.asarray(data_inputs_x1[batch_start:batch_start + batch_size])
            batch_x2s = np.asarray(data_inputs_x2[batch_start:batch_start + batch_size])
            # Run optimizer
            _, summary = sess.run([optimizer, merged], feed_dict={x1: batch_x1s, x2: batch_x2s})
            train_writer.add_summary(summary, batch_i)

        # Check accuracy against validation data
        val_accuracy, val_loss = sess.run([accuracy, loss], feed_dict={x1: np.asarray(data_inputs_x1), x2: np.asarray(data_inputs_x2)})
        print("After epoch {}, Loss: {}, Accuracy: {}".format(epoch_i+1, val_loss, val_accuracy))


    test_accuracy, test_loss = sess.run([accuracy, loss], feed_dict={x1: np.asarray(data_inputs_x1), x2: np.asarray(data_inputs_x2)})
    print ("TEST LOSS: {}, TEST ACCURACY: {}".format(test_loss, test_accuracy))

    g = tf.get_default_graph()
    saver.save(sess, 'savedgraph')