python - 值错误 : Cannot convert a partially known TensorShape to a Tensor: (? ,)

标签 python machine-learning tensorflow svm

我收到错误,指定为“ValueError:无法将部分已知的 TensorShape 转换为张量:(?,)” 我只想获得预测结果,但无法运行 fit 方法。(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()))

最佳答案

我不知道是否还有其他问题,但您遇到的一个问题是 SVMexample_id_column 参数需要是一个字符串。它是表示示例 ID 的特征列的名称。在您的代码中它是一个整数张量。请参阅https://www.tensorflow.org/api_docs/python/tf/contrib/learn/SVM

关于python - 值错误 : Cannot convert a partially known TensorShape to a Tensor: (? ,),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47227727/

相关文章:

python - Keras:如何将 2 个张量与动态形状连接起来?

python - Django - 如何按特定顺序对对象进行排序?

javascript - Django 中的 URL 检测和缩短

python - pandas 条件语句问题

machine-learning - 为什么神经网络对排列标签给出相同的精度?

python - 如何获得FB Prophet的特征重要性?

python - 将泛型子类的类型推断为将自身作为类型

python - TfidfVectorizer 是否隐式地对大型数据集的拟合输出进行阈值处理?

tensorflow - 尺寸必须相等,但 'MatMul_15' 的尺寸为 1024 和 3136 (op : 'MatMul' ) with input shapes: [100, 1024], [3136,100]

python - 如何将给定索引处的值(多个索引)插入到张量中?