无法重塑张量

时间:2019-12-24 15:46:10

标签: python python-3.x tensorflow machine-learning

我建立了此线性回归模型,但出现此错误-

ValueError: Cannot reshape a tensor with 1152 elements to shape [128,6876] (880128 elements) for 'linear/linear_model/linear_model/linear_model/x/Reshape' (op: 'Reshape') with input shapes: [128,9], [2] and with input tensors computed as partial shapes: input[1] = [128,6876].

由于我没有定义批量大小,因此我推断出该错误可能是因为最大模型接受的大小为128。 这是我的代码:-

import numpy as np
import pandas as pd
import tensorflow as tf
from itertools import *
import itertools
from sklearn.preprocessing import LabelEncoder

training_set_n = pd.read_csv("C:/Users/neelg/Documents/Jupyter/Tic_Tac_cls/tic_tac_train1.csv").values

test_set_n = pd.read_csv("C:/Users/neelg/Documents/Jupyter/Tic_Tac_cls/tic_tac_test.csv").values

prediction_set_n = pd.read_csv("C:/Users/neelg/Documents/Jupyter/Tic_Tac_cls/tic_tac_predict.csv").values

def prepare_data(df):
        X_train = df[:, 0:9]
        y_train = df[:, 9:10]
        return X_train, y_train

X_train, y_train = prepare_data(training_set_n)
X_test, y_test = prepare_data(test_set_n)
x_predict = prediction_set_n[:, 0:9]

feature_columns = [tf.feature_column.numeric_column('x', shape=X_train.shape)]

estimator = tf.compat.v1.estimator.LinearRegressor(feature_columns=feature_columns, model_dir="train1")

train_input = tf.compat.v1.estimator.inputs.numpy_input_fn(x={"x": X_train},
           y = y_train,
           shuffle=True,
           num_epochs=None)

eval_input = tf.compat.v1.estimator.inputs.numpy_input_fn(
       x={"x": X_test},
       y=y_test,
       shuffle=False,
       batch_size=1,
       num_epochs=1)

test_input = tf.compat.v1.estimator.inputs.numpy_input_fn(
        x={"x": x_predict},
        batch_size=1,
        num_epochs=1,
        shuffle=False)

TB_train = estimator.train(input_fn=train_input, steps=1000)
TB_test = estimator.evaluate(eval_input,steps=None)

train_data的形状是此(764, 9)。也, 因为在这个区块中

train_input = tf.compat.v1.estimator.inputs.numpy_input_fn(x={"x": X_train},
           y = y_train,
           shuffle=True,
           num_epochs=None)

即使未提供任何内容,batch_size仍为128。

我检查了其他参数,它们似乎按预期工作,那么模型返回的数不能超过128吗?还是有其他错误涉及变量的形状不正确

0 个答案:

没有答案