【问题标题】:Tensorflow : ValueError: Dimensions must be equalTensorflow:ValueError:尺寸必须相等
【发布时间】:2018-08-29 14:17:46
【问题描述】:

我运行了tutorial 的代码,但出现以下错误

我读了一些类似的帖子,但它并没有真正帮助我

ValueError: 尺寸必须相等,但为 128 和 364 'RNN_forward/rnn/while/rnn/multi_rnn_cell/cell_0/basic_lstm_cell/MatMul_1'(操作:'MatMul'),输入形状:[250,128]、[364,256]。

这是教程末尾的代码:

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)

我更改了应用分析的数据集,并尝试对其进行调整。 你知道我该如何解决这个错误吗?

我读过一些类似的帖子,但它并没有真正帮助我。

非常感谢

【问题讨论】:

  • 能否请您也打印 n_words 值
  • n_words 值为 5971

标签: python tensorflow


【解决方案1】:

在浏览了教程的链接后,我发现了这个link of the same issue

建议将你的代码与this repository 合并。

试一试,如果它解决了问题,请告诉我:)

【讨论】:

  • 谢谢 :) ,我会试试的
  • 非常感谢克里希纳,它帮了我很多:)
  • 很高兴知道这一点。如果它对你有帮助,你会将我的回答标记为“接受”吗?(如果你喜欢它,也可以投票):)
【解决方案2】:

感谢post,我解决了这个问题,我替换了这段代码:

  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)

通过该代码:

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

通过添加以下功能:

 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-18
    • 2021-11-08
    • 1970-01-01
    • 2021-08-29
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多