Tflearn model.predict 无法提供形状 (1, 1, 17) 的值
Tflearn model.predict Cannot feed value of shape (1, 1, 17)
我对 Tensorflow 和 tflearn 很陌生
到目前为止,我已经学习了几个教程,并一直在尝试将 tflearn titanic 示例应用于动物园动物数据集 ( http://archive.ics.uci.edu/ml/datasets/Zoo )。训练效果很好,但是当我尝试对输入的数据使用 model.predict 时,出现以下错误
无法为 Tensor '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])
我试过使用重塑,但效果不佳。我使用搜索发现的类似问题在训练阶段出现此错误,而不是预测。
原来我没有仔细看数据集中的列数...
如果其他人遇到类似问题或使用此示例练习机器学习,这里是工作代码。
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])
我对 Tensorflow 和 tflearn 很陌生 到目前为止,我已经学习了几个教程,并一直在尝试将 tflearn titanic 示例应用于动物园动物数据集 ( http://archive.ics.uci.edu/ml/datasets/Zoo )。训练效果很好,但是当我尝试对输入的数据使用 model.predict 时,出现以下错误
无法为 Tensor '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])
我试过使用重塑,但效果不佳。我使用搜索发现的类似问题在训练阶段出现此错误,而不是预测。
原来我没有仔细看数据集中的列数... 如果其他人遇到类似问题或使用此示例练习机器学习,这里是工作代码。
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])