) to a Tensor"-6ren"> ) to a Tensor"-从 Tensorflow 示例中获取必要的代码来对结构化数据进行分类 here这样我就可以学习数字列的训练;我收到以下错误: ValueError: Attempt to convert a valu-6ren">
gpt4 book ai didi

pandas - tensorflow 2 "Attempt to convert a value (63) with an unsupported type () to a Tensor"

转载 作者:行者123 更新时间:2023-11-30 09:41:54 25 4
gpt4 key购买 nike

从 Tensorflow 示例中获取必要的代码来对结构化数据进行分类 here这样我就可以学习数字列的训练;我收到以下错误:

ValueError: Attempt to convert a value (63) with an unsupported type () to a Tensor.

虽然我想我可以尝试将数据帧中的特定值转换为与张量一起使用(如果这甚至可以工作),但当代码在 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)

最佳答案

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

关于pandas - tensorflow 2 "Attempt to convert a value (63) with an unsupported type (<class ' numpy.int6 4'>) to a Tensor",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57519500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com