Reputation: 441
I wanna use CNN for 1D data, so decide to use conv1d
layer. First two layers goes good, but when I create second conv layer, I have and error:
ValueError: Dimensions must be equal, but are 1 and 586 for 'conv1_43/conv1d/Conv2D' (op: 'Conv2D') with input shapes: [586,1,1040,1], [1,5,586,6].
This is my date shape:
trainX = dataX[0:616]
trainY = dataY[0:616]
testX = dataX[616:646]
testY = dataY[616:646]
trainX = np.expand_dims(trainX, axis=2)
testX = np.expand_dims(testX, axis=2)
#final shapes: train:(586,1040,1) test:(30,1040,1)
There's code:
def new_conv_layer(input, num_input_channels, filter_size, num_filters, name):
with tf.variable_scope(name) as scope:
# Shape of the filter-weights for the convolution
shape = [filter_size, num_input_channels, num_filters]
# Create new weights (filters) with the given shape
weights = tf.Variable(tf.truncated_normal(shape, stddev=0.05))
# Create new biases, one for each filter
#biases = tf.Variable(tf.constant(0.05, shape=[num_filters]))
# TensorFlow operation for convolution
layer = tf.nn.conv1d(input, weights, 1, 'SAME')
# Add the biases to the results of the convolution.
#layer += biases
return layer, weights
# Function for creating a new ReLU Layer
def new_relu_layer(input, name):
with tf.variable_scope(name) as scope:
# TensorFlow operation for convolution
layer = tf.nn.relu(input)
return layer
# Convolutional Layer 1
layer_conv1, weights_conv1 = new_conv_layer(trainX, num_input_channels=586, filter_size=5, num_filters=6, name ="conv1")
# Pooling Layer 1new_pool_layer
layer_pool1 = max_pooling1d(layer_conv1, 3, 1, name="pool1")
# RelU layer 1
layer_relu1 = new_relu_layer(layer_pool1, name="relu1")
# Convolutional Layer 2
layer_conv2, weights_conv2 = new_conv_layer(input=layer_relu1, num_input_channels=1, filter_size=5, num_filters=16, name= "conv2")
# Pooling Layer 2
layer_pool2 = max_pooling1d(layer_conv2, 2, 1, name="pool2")
# RelU layer 2
layer_relu2 = new_relu_layer(layer_pool2, name="relu2")
What is a problem?
Upvotes: 0
Views: 996
Reputation: 17201
The input
and kernel
sizes to the nn.conv1d
is not right.
From the API doc,
Input tensor should be of shape:
[batch, in_width, num_input_channels]
Kernel/weights should be of shape:
[filter_width, num_input_channels, out_channels]
The inputs are of shape [586,1,1040,1]
and should be [586, 1040, 1]
, and the kernel has the num_input_channels
defined wrongly when calling the new_conv_layer
. It should be 1 in the first call and 6 in the next call.
Upvotes: 1