【问题标题】:Tensorflow 2 "Attempt to convert a value (63) with an unsupported type (<class 'numpy.int64'>) to a Tensor"Tensorflow 2“尝试将具有不受支持的类型 (<class 'numpy.int64'>) 的值 (63) 转换为张量”
【发布时间】:2019-12-22 11:24:18
【问题描述】:

从 Tensorflow 示例中获取必要的代码,用于对结构化数据进行分类here,以便我可以学习在数值列上进行训练;我收到以下错误:

ValueError: 尝试使用不受支持的类型转换值 (63) () 到张量。

虽然我想我可以尝试将数据框中的特定值转换为与张量一起使用(如果这甚至可以工作的话),但由于代码在 Colab 中工作但在 PyCharm 中引发错误,因此必须有其他事情发生。

import pandas as pd
import tensorflow as tf
from tensorflow import feature_column
from tensorflow.python.keras import layers
from sklearn.model_selection import train_test_split

URL = 'https://storage.googleapis.com/applied-dl/heart.csv'
dataframe = pd.read_csv(URL)
dataframe.head()

train, test = train_test_split(dataframe, test_size=0.2)
train, val = train_test_split(train, test_size=0.2)
print(len(train), 'train examples')
print(len(val), 'validation examples')
print(len(test), 'test examples')

# A utility method to create a tf.data dataset from a Pandas Dataframe
def df_to_dataset(dataframe, shuffle=True, batch_size=32):
  dataframe = dataframe.copy()
  labels = dataframe.pop('target')
  ds = tf.data.Dataset.from_tensor_slices((dict(dataframe), labels))
  if shuffle:
    ds = ds.shuffle(buffer_size=len(dataframe))
  ds = ds.batch(batch_size)
  return ds

feature_columns = []

# numeric cols
for header in ['age', 'trestbps', 'chol', 'thalach', 'oldpeak', 'slope', 'ca']:
  feature_columns.append(feature_column.numeric_column(header))

feature_layer = tf.keras.layers.DenseFeatures(feature_columns)

batch_size = 32
train_ds = df_to_dataset(train, batch_size=batch_size)
val_ds = df_to_dataset(val, shuffle=False, batch_size=batch_size)
test_ds = df_to_dataset(test, shuffle=False, batch_size=batch_size)

"""## Create, compile, and train the model"""

model = tf.keras.Sequential([
  feature_layer,
  layers.Dense(128, activation='relu'),
  layers.Dense(128, activation='relu'),
  layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'],
              run_eagerly=True)

model.fit(train_ds,
          validation_data=val_ds,
          epochs=5)

loss, accuracy = model.evaluate(test_ds)
print("Accuracy", accuracy)

【问题讨论】:

    标签: pandas tensorflow machine-learning


    【解决方案1】:

    Numpy 似乎已损坏。卸载 Numpy 并重新安装后,程序运行正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多