Shape must be rank 1 but is rank 2 tflearn error
In Tensorflow, rank is the number of dimensions of a tensor (not similar to the matrix rank). As an example, following tensor has a rank of 2.
t1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(t1.shape) # prints (3, 3)
Moreover, following tensor has a rank of 3.
t2 = np.array([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
print(t2.shape) # prints (2, 2, 3)
Since tflearn is build on top of Tensorflow, inputs should not be tensors. I have modified your code as follows and commented where necessary.
# Target label used for training
labels = np.array(data[label], dtype=np.float32)
# Reshape target label from (6605,) to (6605, 1)
labels =np.reshape(labels,(-1,1)) #makesure the labels has the shape of (?,1)
# Data for training minus the target label.
data = np.array(data.drop(label, axis=1), dtype=np.float32)
data = np.reshape(data,(-1,32)) #makesure the data has the shape of (?,32)
# DNN
net = tflearn.input_data(shape=[None, 32])
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 1, activation='softmax')
net = tflearn.regression(net)
# Define model.
model = tflearn.DNN(net)
model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)
Hope this helps.
Comments
-
buydadip almost 2 years
I am using a DNN provided by
tflearn
to learn from some data. Mydata
variable has a shape of(6605, 32)
and mylabels
data has a shape of(6605,)
which I reshape in the code below to(6605, 1)
...# Target label used for training labels = np.array(data[label], dtype=np.float32) # Reshape target label from (6605,) to (6605, 1) labels = tf.reshape(labels, shape=[-1, 1]) # Data for training minus the target label. data = np.array(data.drop(label, axis=1), dtype=np.float32) # DNN net = tflearn.input_data(shape=[None, 32]) net = tflearn.fully_connected(net, 32) net = tflearn.fully_connected(net, 32) net = tflearn.fully_connected(net, 1, activation='softmax') net = tflearn.regression(net) # Define model. model = tflearn.DNN(net) model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)
This gives me a couple of errors, the first is...
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'strided_slice' (op: 'StridedSlice') with input shapes: [6605,1], [1,16], [1,16], [1].
...and the second is...
During handling of the above exception, another exception occurred:
ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice' (op: 'StridedSlice') with input shapes: [6605,1], [1,16], [1,16], [1].
I have no idea what
rank 1
andrank 2
are, so I do not have an idea as to how to fix this issue.-
desertnaut over 6 yearsTry dropping the reshaping of
labels
; does the error persist? It yes pls provide a sample of your data (also, any link where this model is provided, as you say, will be useful)
-