K. Project
K. Project

Reputation: 112

errors while feeding data through feed-dict in python

In the code below, test is a 3-d numpy Array, with varying row sizes for the matrices in the array. I can not feed this 3-d array in feed dict because of this. Is there any way to fix this?

This is the error:

Traceback (most recent call last):
  File "test.py", line 
83, in <module>
    sess.run(iter.initializer, feed_dict={M: test, v_a: va, y: label})
  File "C:\WinPython-64bit-3.6.3.0Qt5\python-3.6.3.amd64\lib\site- 
packages\tensorflow\python\client\session.py", line 905, in run
    run_metadata_ptr)
  File "C:\WinPython-64bit-3.6.3.0Qt5\python-3.6.3.amd64\lib\site- 
packages\tensorflow\python\client\session.py", line 1106, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
  File "C:\WinPython-64bit-3.6.3.0Qt5\python-3.6.3.amd64\lib\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.

This is my code:

def multilayer_perceptron(m_i, v_a, weights, biases):

    #Hidden layers

    layer_1 = tf.add(tf.add(tf.matmul(weights['W1'], m_i), 
    tf.matmul(weights['W2'], v_a)), biases['b1'])
    layer_1 = tf.nn.tanh(layer_1)

    layer_2 = tf.matmul(weights['W3'], layer_1)

    return layer_2

def ModelA(M, v_a, weights, biases, d):

    N = sess.run(tf.size(M))/d

    c = multilayer_perceptron((tf.slice(M, [0, 0], ([d, 1]))), v_a, weights, 
     biases)

    for i in range(1, N):

        c = tf.concat([c, multilayer_perceptron((tf.slice(M, [0, i], [d, 
            1])), v_a, weights, biases)], axis=0)

    alpha = tf.nn.softmax(tf.reshape(c, [-1]))

    v_ns = tf.matmul(M, tf.reshape(alpha, [N, 1]))

    layer_3 = tf.add(tf.matmul(weights['W4'], v_ns), biases['b2'])
    v_ms = tf.nn.tanh(layer_3)

    layer_4 = tf.add(tf.matmul(weights['W5'], v_ms), biases['b3'])
    pred = tf.nn.softmax(tf.reshape(layer_4, [-1]))

    return pred

def generator():
    for el in test:
        yield el

# Placeholders and Constants
number_of_classes = 3
M = tf.placeholder(tf.float32, name='M')
d = 50     #rows M matrix = d
y = tf.placeholder(tf.float32, shape=[1, number_of_classes], name='y')
v_a = tf.placeholder(tf.float32, shape=[d, 1], name='v_a')

# Hyperparameters
learning_rate = 0.01
training_epochs = 10

# Variables
weights = {
    'W1': tf.Variable(tf.truncated_normal(shape=[d, d])),
    'W2': tf.Variable(tf.truncated_normal(shape=[d, d])),
    'W3': tf.Variable(tf.truncated_normal(shape=[1, d])),
    'W4': tf.Variable(tf.truncated_normal(shape=[d, d])),
    'W5': tf.Variable(tf.truncated_normal(shape=[number_of_classes, d]))
}

biases = {
    'b1': tf.Variable(tf.truncated_normal([d, 1])),
    'b2': tf.Variable(tf.truncated_normal([d, 1])),
    'b3': tf.Variable(tf.truncated_normal([number_of_classes, 1]))
}

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

test = np.array([[[[1.], [2.], [3.]],[[4.], [5.], [6.]], [[7.], [8.], 
        [9.]]], [[[6.], [2.], [4.]], [[2.], [1.], [8.]]], [[[7.], [6.], 
        [2.]], [[2.], [4.], [2.]], [[3.], [8.], [9.]], [[1.], [2.], [1.]]]])
label = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]])
va = np.array([[[1.], [2.], [3.]], [[2.], [1.], [8.]], [[1.], [2.], [1.]]])


dataset = tf.data.Dataset().from_generator(generator, 
output_types=tf.float32)
iter = dataset.make_initializable_iterator()

sess.run(iter.initializer, feed_dict={M: test, v_a: va, y: label})
cost_function = 
    tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=ModelA(M, 
        v_a, weights, biases, d), labels=label))
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(cost_function)

print('Training...')
for i in range(training_epochs):
    tot_loss = 0
    trainings, loss_value = sess.run([optimizer, cost_function])
    tot_loss += loss_value
    print("Iter: {}, Loss: {:.4f}".format(i, tot_loss))

Upvotes: 0

Views: 55

Answers (1)

jv3768
jv3768

Reputation: 322

Don't use sess.run(...) while you are still doing tf.operation, this will make your code much slower. Varying row sizes might be solved by filling the matrix with zeroes and making it 3D.

Upvotes: 1

Related Questions