多像元的值错误问题尺寸必须相等,但必须为20和13

时间:2018-12-13 18:11:50

标签: python tensorflow multidimensional-array valueerror word-embedding

我正在使用python 3.6.5和tensorflow 1.8.0 目前,神经元的Nr为10,在此示例中为3

我已经建立了一个递归神经元网络,现在想改善它。我需要帮助!

这里有一段代码摘录来重现我的错误:您还可以用LSTM或GRU替换BasicRNN以获取其他消息。

import numpy      as np
import tensorflow as tf  
batch_size = 10
nr_inputs = 3  
nr_outputs  = 4   
nr_steps = 4             
nr_layers  = 2
def  mini_batch ( Xdata, ydata, batch_size ) :  
    global  global_counter
    result      = None
    Xbatch      = np.zeros( shape=[batch_size, nr_steps,  nr_inputs],  dtype = np.float32 )
    ybatch      = np.zeros( shape=[batch_size, nr_outputs],            dtype = np.float32 )
    return  Xbatch, ybatch
X = tf.placeholder( tf.float32, [ None, nr_steps,    nr_inputs ] )
y = tf.placeholder( tf.float32, [ None, nr_outputs ]             )
neurons = tf.contrib.rnn.BasicRNNCell(num_units = 10)
neurons = tf.contrib.rnn.MultiRNNCell( [neurons] * nr_layers, state_is_tuple = True )
X_train = np.zeros( shape=[1000, nr_steps,  nr_inputs],  dtype = np.float32 )
y_train = np.zeros( shape=[1000, nr_outputs],            dtype = np.float32 )
X_test      = np.zeros( shape=[1000,  nr_steps,  nr_inputs],  dtype = np.float32 )
y_test      = np.zeros( shape=[1000,  nr_outputs],            dtype = np.float32 )
rnn_outputs, rnn_states = tf.nn.dynamic_rnn( neurons, X, dtype=tf.float32 )
logits = tf.contrib.layers.fully_connected( inputs        = rnn_states, num_outputs = nr_outputs, activation_fn = None )
xentropy    = tf.nn.sigmoid_cross_entropy_with_logits( labels = y,  logits = logits )
loss        = tf.reduce_mean( xentropy )
optimizer   = tf.train.AdamOptimizer( learning_rate = 0.01 )
training_op = optimizer.minimize( loss )
init         = tf.global_variables_initializer()
with tf.Session() as sess :
    init.run()
    global_counter = 0
    for epoch in range(100) :
        for iteration in range( 4) :
            X_batch, y_batch = mini_batch ( X_train, y_train, batch_size )
            sess.run( training_op, feed_dict={ X : X_batch,  y : y_batch } ) 
        loss_train = loss.eval( feed_dict={ X : X_batch,  y : y_batch } )
        loss_test  = loss.eval( feed_dict={ X : X_test,   y : y_test  } )
    sess.close()

我正在尝试neurons = tf.contrib.rnn.MultiRNNCell([neurons]*nr_layers, state_ist_tuple = True)

并收到错误

ValueError: Dimensions must be equal, but are 20 and 13 for 'rnn/.../MatMul1'(op 'MatMul') with input shapes [?,20], [13, 10] for a tf.contrib.rnn.BasicRNNCell(num_units = nr_neurons)

with input shapes [?,20], [13, 20] for a tf.contrib.rnn.GRUCell(num_units = nr_neurons)

with input shapes [?,20], [13, 40] for a tf.contrib.rnn.BasicLSTMCell(num_units = nr_neurons, state_is_tuple = True)

MatMul_1中是否有错误?有没有人遇到过类似的问题? 非常感谢!

1 个答案:

答案 0 :(得分:0)

应该而不是多次使用BasicRNNCell实例,而应该为每个RNN层创建一个实例-例如,以这种方式:

neurons = [tf.contrib.rnn.BasicRNNCell(num_units=10) for _ in range(nr_layers)]
neurons = tf.contrib.rnn.MultiRNNCell( neurons, state_is_tuple = True )

此外,您的代码上还有其他错误。rnn_states是一个包含单元格状态和隐藏状态的元组,其形状为((None,10),(None,10))。 我假设您要使用隐藏状态,请替换它:

logits = tf.contrib.layers.fully_connected( inputs = rnn_states[1], num_outputs = nr_outputs, activation_fn = None )

没关系!