简体   繁体   中英

Tensorflow : ValueError: Dimensions must be equal

I ran the code of that tutorial and I got the following error

I read some similar posts but it didn't really help me

ValueError: Dimensions must be equal, but are 128 and 364 for 'RNN_forward/rnn/while/rnn/multi_rnn_cell/cell_0/basic_lstm_cell/MatMul_1' (op: 'MatMul') with input shapes: [250,128], [364,256].

Here is the code at the end of the tutorial :

n_words = len(word_index)
embed_size = 300
batch_size = 250
lstm_size = 128
num_layers = 2
dropout = 0.5
learning_rate = 0.001
epochs = 100
multiple_fc = False
fc_units = 256


# Train the model with the desired tuning parameters# Train  
for lstm_size in [64,128]:
    for multiple_fc in [True, False]:
        for fc_units in [128, 256]:
            log_string = 'ru={},fcl={},fcu={}'.format(lstm_size,
                                                      multiple_fc,
                                                      fc_units)
            model = build_rnn(n_words = n_words, 
                              embed_size = embed_size,
                              batch_size = batch_size,
                              lstm_size = lstm_size,
                              num_layers = num_layers,
                              dropout = dropout,
                              learning_rate = learning_rate,
                              multiple_fc = multiple_fc,
                              fc_units = fc_units)            
            train(model, epochs, log_string)

I changed the dataset on which the analysis is applied and I tried to adapt it. Do you have an idea how I could solve that error ?

I read some similar posts but it didn't really help me.

Thank you very much

After going through the link of the tutorial, i found this link of the same issue .

It suggests to merge ur code with this repository .

Give it a try and let me know if it fixes the issue :)

I fixed the problem thanks to that post , I replaced this code :

  with tf.name_scope('RNN_layers'):
   lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
   drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
   cell = tf.contrib.rnn.MultiRNNCell([drop] * num_layers)

by that code :

with tf.name_scope('RNN_layers'):
 cell = tf.contrib.rnn.MultiRNNCell([lstm_cell(lstm_size, keep_prob) for _ in 
 range(num_layers)])

by adding the following function also :

 def lstm_cell(lstm_size, keep_prob):
    lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
    drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
    return drop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM