在下面的代码中,test
是一个3维的numpy数组,阵列中的矩阵具有不同的行大小。因为这个原因,我无法在feed dict中提供这个3-d数组。有什么方法可以解决这个问题吗?
这是错误:
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.
这是我的代码:
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))
答案 0 :(得分:1)
当你还在使用tf.operation
时,不要使用sess.run(...),这会使你的代码慢得多。可以通过用零填充矩阵并使其成为3D来解决变化的行大小。