1

我正在使用由tflearn提供的DNN從一些數據中學習。我data變量的(6605, 32)的形狀和我labels數據具有(6605,),我在下面的代碼(6605, 1)重塑形狀......形狀必須是1級,但是是2級tflearn錯誤

# 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) 

這給了我一對夫婦的錯誤,首先是...

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].

...第二個是...

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].

我不知道什麼rank 1rank 2是,所以我不知道如何解決這個問題。

+0

嘗試刪除「標籤」的整形;錯誤是否持續?它是提供一個你的數據樣本(也可以是任何提供此模型的鏈接,如你所說,將是有用的) – desertnaut

回答

2

在Tensorflow中,秩是張量的維數(與矩陣秩不相似)。作爲一個例子,下面的張量具有2

t1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 
print(t1.shape) # prints (3, 3) 

此外秩,以下張量具有3.

t2 = np.array([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]) 
print(t2.shape) # prints (2, 2, 3) 

由於tflearn是建立在Tensorflow的頂部,輸入不應該秩是張量。我已經修改了你的代碼,並在必要時進行了評論。

# 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) 

希望這會有所幫助。

相關問題