多像元的值错误问题尺寸必须相等,但必须为20和13 [英] Value Error problem with multicell Dimensions must be equal, but are 20 and 13

查看:147
本文介绍了多像元的值错误问题尺寸必须相等,但必须为20和13的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working with python 3.6.5 and tensorflow 1.8.0 Nr of neurons are 10 at the moment, input in this example is 3

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

I have already build a recurrent neuronal network and now wanted to improve it. I need some help!

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

Here is a little excerpt of the code to reproduce my error: You can also replace BasicRNN by LSTM or GRU to get the other messages.

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中是否有错误?有没有人遇到过类似的问题? 非常感谢!

is there an error in the MatMul_1? Has anyone ever had similar problems? Thank you so much!

推荐答案

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

Instead of using the BasicRNNCell instance multiple times,one instance per RNN layer should be created - for example in this way:

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)). 我假设您要使用隐藏状态,请替换它:

In addition, there are other mistakes on your codes.rnn_states is a tuple containing cell state and hidden state, and its shape is ((None,10),(None,10)). I assume you want to use hidden state,replace it:

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

没关系!

这篇关于多像元的值错误问题尺寸必须相等,但必须为20和13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆