'tensorflow tf.nn.bidirectional_dynamic_rnn error after tf.reshape

My tensorflow version is 1.3.0 .

My python version is 3.5.

I implement CNN followed by bid-LSTM. and I run code on CPU.

After implementing CNN, pool2's shape will be [batch_size(None), None, 106, 64]. Then tf.reshape(pool2, [-1, tf.shape(pool2)[1], tf.shape(pool2)[2]xtf.shape(pool2)[3]]) . I hope tf.reshape can reshape 4D into 3D on pool2. And then feed it bid-LSTM, but tf.nn.bidirectional_dynamic_rnn happen wrong.

It says "Input size (depth of inputs) must be accessible via shape inference," ValueError: Input size (depth of inputs) must be accessible via shape inference, but saw value None.

I haven't found the solution to the problem for a long time. Maybe I use wrong keyword to search on Internet. Or give some right keyword to me.

    x = tf.placeholder('float', shape=[None, None, 108])
    y = tf.placeholder('float')
    n_steps = tf.placeholder('int64')
    def CNN(x):     
        input_layer = tf.reshape(x, [-1, tf.shape(x)[1], 108, 1])
        conv1 = tf.layers.conv2d(inputs=input_layer, filters=32, kernel_size=[5, 3], padding="same", activation=tf.nn.relu)
        pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=1)
        conv2 = tf.layers.conv2d(inputs=conv1,  filters=64, kernel_size=[3, 3], padding="same", activation=tf.nn.relu)
        pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=1)
        output = tf.reshape(pool2, [-1, tf.shape(pool2)[1], tf.shape(pool2)[2]*tf.shape(pool2)[3]])
        return output

    def recurrent_neural_network(x):
        layer1 = {'weights':tf.Variable(tf.random_normal([rnn_size*2,n_classes])),'biases':tf.Variable(tf.random_normal([n_classes]))}
        lstm_fw_cell = tf.nn.rnn_cell.BasicLSTMCell(rnn_size,state_is_tuple=True)
        lstm_bw_cell = tf.nn.rnn_cell.BasicLSTMCell(rnn_size,state_is_tuple=True)   
        outputs, states = tf.nn.bidirectional_dynamic_rnn(cell_fw=lstm_fw_cell, cell_bw=lstm_bw_cell, inputs=x, dtype=tf.float32)  #[batch_size, max_time, cell_output_size]

        outputs = tf.concat(outputs, 2)
        max_length = tf.shape(outputs)[1]
        outputs = tf.reshape(outputs, [-1, rnn_size*2])
        prediction = tf.matmul(outputs,layer1['weights']) + layer1['biases']
        prediction = tf.reshape(prediction, [-1, max_length, n_classes])
        return prediction


    def train_neural_network(x):
        CNN_result = CNN(x)
        prediction = recurrent_neural_network(CNN_result)
        tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y)
        cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y) )
        optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())

            for epoch in range(hm_epochs):
                epoch_loss = 0
                i=0
                while i < len(train_X):
                    start = i
                    end = i+batch_size

                    batch_x = train_X[start:end]
                    batch_y = train_Y[start:end]
                    batch_sen_len = train_sen_len[start:end]
                    max_batch_sen_len = max(batch_sen_len)
                    #padding zero
                    for j in range(len(batch_x)):
                        k = max_batch_sen_len - len(batch_x[j])
                        for _ in range(k):
                            batch_x[j].append([0]*108)
                            batch_y[j].append([0]*48)

                    _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y, n_steps: batch_sen_len})
                    epoch_loss += c
                    i+=batch_size


                print('Epoch', epoch+1, 'completed out of',hm_epochs,'loss:',epoch_loss)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source