Machine learning 输入数据的Tensorflow BiRNN动态维度大小

Machine learning 输入数据的Tensorflow BiRNN动态维度大小,machine-learning,computer-vision,tensorflow,artificial-intelligence,recurrent-neural-network,Machine Learning,Computer Vision,Tensorflow,Artificial Intelligence,Recurrent Neural Network,我对构建BiRNN网络的输入数据有异议 我正在创建车牌检测系统,如下所述: 我已经到了“4.2.3序列标签”部分,我需要用(图像的总计数,无,256)形状的数据集来训练BiRNN,无,因为它是图像的长度,并且对于数据集中的每个图片都是不同的 假设我有3000张图片。然后形状看起来像: 形状:(3000,)但真的是(3000,无,256) 所以我从中得到了示例代码 所以我甚至在努力训练我的RNN。我不明白我需要如何构造输入数据/模型、输入占位符、变量等来实现任何培训过程 据我所知,一切都应该正常

我对构建BiRNN网络的输入数据有异议

我正在创建车牌检测系统,如下所述:

我已经到了“4.2.3序列标签”部分,我需要用(图像的总计数,无,256)形状的数据集来训练BiRNN,无,因为它是图像的长度,并且对于数据集中的每个图片都是不同的

假设我有3000张图片。然后形状看起来像:

形状:(3000,)但真的是(3000,无,256)

所以我从中得到了示例代码

所以我甚至在努力训练我的RNN。我不明白我需要如何构造输入数据/模型、输入占位符、变量等来实现任何培训过程

据我所知,一切都应该正常。我的代码:

reset_graph()

'''

Dataset : (10000, 784)
Labels : (10000, 10)

To classify images using a bidirectional reccurent neural network, we consider
every image row as a sequence of pixels. Because MNIST image shape is 28*28px,
we will then handle 28 sequences of 28 steps for every sample.
'''

# Parameters
learning_rate = 0.001
training_iters = 100 # 100000
display_step = 10
batch_size = 40

# Network Parameters
n_input = 256 # data inpit size/256D
n_steps = 256 # timesteps
n_hidden = 200 # hidden layer num of features
n_classes = 36 # MNIST total classes (0-9 digits and a-z letters)

# tf Graph input
x = tf.placeholder("float", [batch_size, None , n_input], name='input_placeholder')
y = tf.placeholder("float", [batch_size, None, n_classes], name='labels_placeholder')

# Define weights
weights = {
    # Hidden layer weights => 2*n_hidden because of foward + backward cells
    'out': tf.Variable(tf.random_normal([2*n_hidden, n_classes]))
}
biases = {
    'out': tf.Variable(tf.random_normal([n_classes]))
}

def BiRNN(x, weights, biases):

    print('Input x',x.get_shape().as_list())
    print('weights[\'out\']', weights['out'].get_shape().as_list())
    print('biases[\'out\']', biases['out'].get_shape().as_list())

    # Prepare data shape to match `bidirectional_rnn` function requirements
    # Current data input shape: (batch_size, n_steps, n_input)
    # Required shape: 'n_steps' tensors list of shape (batch_size, n_input)

    # Permuting batch_size and n_steps
    #x = tf.transpose(x, [1, 0, 2])
    #print('Transposed x',x.get_shape().as_list())
    # Reshape to (n_steps*batch_size, n_input)
    x = tf.reshape(x, [-1, n_steps])
    print('Reshaped x',x.get_shape().as_list())

    # Split to get a list of 'n_steps' tensors of shape (batch_size, n_input)
    x = tf.split(0, n_input, x)
    print(len(x),'of [ ',x[0],' ] kinds')

    # Define lstm cells with tensorflow
    # Forward direction cell
    lstm_fw_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
    # Backward direction cell
    lstm_bw_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)

    # Get lstm cell output
    outputs, _, _ = rnn.bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x, dtype=tf.float32)
    print( len(outputs),'of [ ',outputs[0],' ] kinds' )

    # Linear activation, using rnn inner loop last output
    ret = tf.matmul(outputs[-1], weights['out']) + biases['out']
    print('ret', ret.get_shape().as_list())

    return ret

pred = BiRNN(x, weights, biases)

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Evaluate model
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# Initializing the variables
init = tf.initialize_all_variables()
输出:

Input x [40, None, 256]
weights['out'] [400, 36]
biases['out'] [36]
Reshaped x [None, 256]
256 of [  Tensor("split:0", shape=(?, 256), dtype=float32)  ] kinds
256 of [  Tensor("concat:0", shape=(?, 400), dtype=float32)  ] kinds
ret [None, 36]
一切都很好。 问题从会话部分开始:

# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    step = 1

    batch_data = batch_gen(batch_size)

    # Keep training until reach max iterations
    while step * batch_size  < training_iters:
        batch_x, batch_y = next(batch_data)
        print(batch_x.shape)
        print(batch_y.shape)
        #m[:,0, None, None].shape
        #Run optimization op (backprop)
        print('Optimizer')
        sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})

        if step % display_step == 0:
            print('Display')
            # Calculate batch accuracy
            acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
            # Calculate batch loss
            loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
            print("Iter " + str(step * batch_size) + ", Minibatch Loss= " + \
                  "{:.6f}".format(loss) + ", Training Accuracy= " + \
                  "{:.5f}".format(acc))
        step += 1
    print("Optimization Finished!")

    # Calculate accuracy for 128 mnist test images
    test_len = 128
    test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))
    test_label = mnist.test.labels[:test_len]
    print("Testing Accuracy:", \
        sess.run(accuracy, feed_dict={x: test_data, y: test_label}))
#启动图形
使用tf.Session()作为sess:
sess.run(初始化)
步骤=1
批次数据=批次生成(批次大小)
#继续训练直到达到最大迭代次数
步骤*批量大小<培训内容:
批次x,批次y=下一个(批次数据)
打印(批处理形状)
打印(批处理形状)
#m[:,0,无,无].形状
#运行优化op(backprop)
打印('优化器')
run(优化器,feed_dict={x:batch_x,y:batch_y})
如果步骤%display\u步骤==0:
打印('显示')
#计算批次精度
acc=sess.run(精度,进给量={x:batch\ux,y:batch\uy})
#计算批量损失
loss=sess.run(成本,feed_dict={x:batch_x,y:batch_y})
打印(“Iter”+str(步进*批量大小)+”,小批量丢失=“+\
“{.6f}”。格式(丢失)+”,训练精度=“+\
“{.5f}”。格式(acc))
步骤+=1
打印(“优化完成!”)
#计算128 mnist测试图像的精度
测试长度=128
test_data=mnist.test.images[:test_len]。重新整形((-1,n_步,n_输入))
test\u label=mnist.test.labels[:test\u len]
打印(“测试精度:”\
sess.run(精度,feed_dict={x:test_data,y:test_label}))
在这里,我得到了以下错误:

(40,)
(40,)
Optimizer

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-96-a53814db8181> in <module>()
     14         #Run optimization op (backprop)
     15         print('Optimizer')
---> 16         sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
     17 
     18         if step % display_step == 0:

/home/nauris/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    715     try:
    716       result = self._run(None, fetches, feed_dict, options_ptr,
--> 717                          run_metadata_ptr)
    718       if run_metadata:
    719         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/home/nauris/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    886                 ' to a larger type (e.g. int64).')
    887 
--> 888           np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
    889 
    890           if not subfeed_t.get_shape().is_compatible_with(np_val.shape):

/home/nauris/anaconda3/lib/python3.5/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
    480 
    481     """
--> 482     return array(a, dtype, copy=False, order=order)
    483 
    484 def asanyarray(a, dtype=None, order=None):

ValueError: setting an array element with a sequence.
(40,)
(40,)
优化器
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
14#运行优化op(backprop)
15打印(“优化器”)
--->16 sess.run(优化器,feed_dict={x:batch_x,y:batch_y})
17
18如果步骤%display\u步骤==0:
/home/nauris/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self、fetches、feed_dict、options、run_元数据)
715尝试:
716结果=self.\u运行(无、取数、输入、选项、,
-->717运行元数据(ptr)
718如果运行\u元数据:
719 proto_data=tf_session.tf_GetBuffer(运行元数据ptr)
/home/nauris/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in_运行(self、handle、fetches、feed_dict、options、run_元数据)
886'到更大的类型(例如int64)。”
887
-->888 np_val=np.asarray(子进纸值,数据类型=子进纸类型)
889
890如果不是子进纸,则获取形状()是否与(np值形状)兼容:
/asarray中的home/nauris/anaconda3/lib/python3.5/site-packages/numpy/core/numeric.py(a,数据类型,订单)
480
481     """
-->482返回数组(a,数据类型,copy=False,order=order)
483
484 def asanyarray(a,数据类型=无,订单=无):
ValueError:使用序列设置数组元素。
任何帮助都将不胜感激。提前谢谢大家


意识到发生错误是因为无法向numpy ndarray提供不一致的维度,如(3000,None,256)。尚未找到任何解决方案。

问题已解决。需要使用稀疏张量和不同的数据结构。问题已解决。需要使用稀疏张量和不同的数据结构。