2017-11-11 118 views
1

我對Tensorflow和tflearn很新穎 到目前爲止,我已經研究了幾個教程,並一直試圖將tflearn泰坦尼克號應用於動物園動物數據集(http://archive.ics.uci.edu/ml/datasets/Zoo)。培訓工作很好,但是當我嘗試對我輸入的數據嘗試使用model.predict時,出現以下錯誤Tflearn model.predict無法提供形狀的值(1,1,17)

無法提供張量'InputData/X:0'的形狀值(1,1,17) ,它具有形狀 '(?16)'

這裏的Python代碼

from __future__ import print_function 

import numpy as np 
import tflearn 

# Load CSV file, indicate that the first column represents labels 
from tflearn.data_utils import load_csv 
data, labels = load_csv('zoo.csv', target_column=-1, 
         categorical_labels=True, n_classes=8) 


# Preprocessing function 
def preprocess(data, columns_to_ignore): 
    # Sort by descending id and delete columns 
    for id in sorted(columns_to_ignore, reverse=True): 
     [r.pop(id) for r in data] 
    return np.array(data, dtype=np.float32) 

# Ignore 'name' and 'ticket' columns (id 1 & 6 of data array) 
to_ignore=[0] 

# Preprocess data 
data = preprocess(data, to_ignore) 

# Build neural network 
net = tflearn.input_data(shape=[None,16]) 
net = tflearn.fully_connected(net, 128) 
net = tflearn.dropout(net, 1) 
net = tflearn.fully_connected(net, 128) 
net = tflearn.dropout(net, 1) 
net = tflearn.fully_connected(net, 8, activation='softmax') 
net = tflearn.regression(net) 


# Define model 
model = tflearn.DNN(net) 
# Start training (apply gradient descent algorithm) 
model.fit(data, labels, n_epoch=1, validation_set=0.1, shuffle=True, batch_size=17, show_metric=True) 

ant = ['ant', 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 8, 0, 0, 0] 
# Preprocess data 
ant = preprocess([ant], to_ignore) 
# Predict surviving chances (class 1 results) 
pred = model.predict([ant]) 
print("Ant is:", pred[0]) 

我使用重塑試過了,沒有相當的工作。我發現使用搜索的類似問題在訓練階段出現此錯誤,而不是預測。

+0

請分享一下''data'&'''vars以及'zoo.csv'的樣本(沒有樣本數據,任何人都可以幫忙......) – desertnaut

回答

0

原來我在數據集的列數上看起來不夠細心...... 如果其他人遇到類似問題或使用此示例來練習機器學習,那麼這裏是工作代碼。

from __future__ import print_function 

import numpy as np 
import tflearn 

# Load CSV file, indicate that the first column represents labels 
from tflearn.data_utils import load_csv 
data, labels = load_csv('zoo.csv', target_column=-1, 
         categorical_labels=True, n_classes=8) 


# Preprocessing function 
def preprocess(data, columns_to_ignore): 
    # Sort by descending id and delete columns 
    for id in sorted(columns_to_ignore, reverse=True): 
     [r.pop(id) for r in data] 
    return np.array(data, dtype=np.float32) 

# Ignore 'name' and 'ticket' columns (id 1 & 6 of data array) 
to_ignore=[0] 

# Preprocess data 
data = preprocess(data, to_ignore) 

# Build neural network 
net = tflearn.input_data(shape=[None,16]) 
net = tflearn.fully_connected(net, 128) 
net = tflearn.dropout(net, 1) 
net = tflearn.fully_connected(net, 128) 
net = tflearn.dropout(net, 1) 
net = tflearn.fully_connected(net, 8, activation='softmax') 
net = tflearn.regression(net) 


# Define model 
model = tflearn.DNN(net) 
# Start training (apply gradient descent algorithm) 
model.fit(data, labels, n_epoch=30, validation_set=0.1, shuffle=True, batch_size=20, show_metric=True) 

ant = [0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 8, 0, 0, 0] 
# Preprocess data 
# ant = preprocess([ant], to_ignore) 
# ant = np.reshape(ant, (1,16)) 
# Predict surviving chances (class 1 results) 
pred = model.predict_label([ant]) 
print("Ant is:", pred[0]) 
相關問題