Tflearn model.predict Невозможно передать значение формы (1, 1, 17)

Я новичок в Tensorflow и tflearn. Пока что я изучил пару руководств и пытался применить пример tflearn titanic к набору данных о животных зоопарка (http://archive.ics.uci.edu/ml/datasets/Zoo). Обучение работает отлично, но когда я пытаюсь использовать model.predict для вводимых данных, я получаю следующую ошибку

Невозможно передать значение формы (1, 1, 17) для тензора 'InputData / X: 0', имеющего форму '(?, 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])

Я пробовал использовать reshape, это не совсем сработало. В аналогичных проблемах, которые я обнаружил при использовании поиска, эта ошибка появляется на этапе обучения, а не при прогнозировании.


person Dmitry Maslov    schedule 11.11.2017    source источник
comment
пожалуйста, поделитесь образцом ваших data & labels варов, а также из zoo.csv (без образцов данных очень сомнительно, что кто-то может помочь ...)   -  person desertnaut    schedule 11.11.2017


Ответы (1)


Оказывается, я недостаточно внимательно смотрел на количество столбцов в наборе данных ... Вот рабочий код, если кто-то еще столкнется с аналогичной проблемой или использует этот пример для практики машинного обучения.

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])
person Dmitry Maslov    schedule 27.11.2017