我收到错误指定" ValueError:无法将部分已知的TensorShape转换为Tensor :(?,)"
我只是想获得预测结果我无法运行拟合方法。(estimator.fit(input_fn=get_input_fn_train(), steps=10000
)我在尝试运行此代码时收到错误。我有10000行和8个int列。我现在删除了字符串列。 (x_train.shape)
- >(8000,8)。y_train.shape
- > (8000,)。谈论什么样的形状?我正在谷歌搜索我找不到有用的东西。我该怎么办?我错过了什么?以下所有代码。感谢。
import pandas as pd
import tensorflow as tf
import numpy as np
import tempfile
from sklearn.model_selection import train_test_split
def split_data(data, rate, label):
data = data.dropna()
train_data, test_data = train_test_split(data, test_size=rate)
train_label = train_data[label]
train_data = train_data.drop(label, 1)
test_label = test_data[label]
test_data = test_data.drop(label, 1)
return train_data, train_label, test_data, test_label
LABEL="Exited"
data = pd.read_csv("Churn_Modelling.csv", skipinitialspace=True, header=0)
data.drop("Surname", axis=1, inplace=True)
data.drop("RowNumber", axis=1, inplace=True)
data.drop("CustomerId", axis=1, inplace=True)
data.drop("Geography", axis=1, inplace=True)
data.drop("Gender", axis=1, inplace=True)
x_train, y_train, x_test, y_test = split_data(data, 0.20, LABEL)
def get_input_fn_train():
input_fn = tf.estimator.inputs.pandas_input_fn(
x=x_train.astype('float64'),
y=y_train.astype('float32'),
shuffle=False
)
return input_fn
def get_input_fn_test():
input_fn = tf.estimator.inputs.pandas_input_fn(
x=x_test.astype('float64'),
y=y_test.astype('float32'),
shuffle=False
)
return input_fn
feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input_fn(
get_input_fn_train())
model_dir = tempfile.mkdtemp()
estimator = tf.contrib.learn.SVM(
example_id_column=tf.constant(np.arange(len(y_train))),
feature_columns=feature_columns, l2_regularization=10.0,model_dir=model_dir)
estimator.fit(input_fn=get_input_fn_train(), steps=10000)
#(I am getting error this line)
results=estimator.evaluate(input_fn=get_input_fn_test(), steps=1)
for key in sorted(results):
print("%s: %s" % (key, results[key]))
pred=list(estimator.predict(input_fn=get_input_fn_test()))
答案 0 :(得分:0)
我不知道是否还有其他问题但是你遇到的一个问题是example_id_column
的{{1}}参数需要是一个字符串。它是表示示例ID的要素列的名称。在你的代码中,它是一个整数Tensor。见https://www.tensorflow.org/api_docs/python/tf/contrib/learn/SVM