python - 想要使用 tensorflow 拆分从 csv 获得的训练和测试数据
问题描述
我想用 tensorflow 拆分 csv 的训练和测试数据,但我没有在 tensor 中找到像 np.loadtxt 这样的订单,并尝试用 numpy 进行拆分并将其转换为 tensor,但出现如下错误:
TypeError: object of type 'Tensor' has no len()
这是我的代码:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
x = tf.convert_to_tensor( np.loadtxt('data.csv', delimiter=','))
y = tf.convert_to_tensor(np.loadtxt('labels.csv', delimiter=','))
x_train, x_test, y_train, y_test = train_test_split(x, y,
test_size=0.25, random_state='')
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape= (426,30,1)),
tf.keras.layers.Dense(126, activation=tf.nn.tanh),
#tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.tanh)
])
model.compile(optimizer='sgd',
loss='mean_squared_error',
metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=5 ) #validation_data
= [x_test, y_test])
model.evaluate(x_test, y_test)
t_predicted = model.predict(x_test)
out_predicted = np.argmax(t_predicted, axis=1)
conf_matrix = tf.confusion_matrix(y_test, out_predicted)
with tf.Session():
print('Confusion Matrix: \n\n', tf.Tensor.eval(conf_matrix,
feed_dict=None, session=None))
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
解决方案
首先加载csv文件,进行拆分然后给Tf拆分结果不是更简单吗?
sklearn.model_selection.train_test_split()
不适用于您从中获取的张量对象tf.convert_to_tensor()
。
颠倒顺序使您的代码在小型测试脚本中工作
x = np.loadtxt('data.csv', delimiter=',')
y = np.loadtxt('labels.csv', delimiter=',')
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)
x_train = tf.convert_to_tensor(x_train)
x_test = tf.convert_to_tensor(x_test)
y_train = tf.convert_to_tensor(y_train)
y_test = tf.convert_to_tensor(y_test)
推荐阅读
- php - 如果 php 会话存在,则从 .htaccess 锁定文件夹下载文件
- c# - Selenium 无法访问网页上的任何元素
- php - 如何使用 PHP 使“日期时间”显示比 PhpMyAdmin 状态提前 4 小时?
- android - 当项目订单发生变化时,如何将我的回收站视图滚动到顶部?
- wix - 由于 WiX 已从 3.6 升级到 3.11,WiX Burn Bootstrapper 立即失败
- json - 如何在没有 Codable 的情况下将 Swift JSON / Dict Key camel 转换为下划线?
- python - Flask - Google Firestore google.api_core.exceptions.Unknown:无 已删除流
- linux - 树莓派内核交叉编译缺少配置选项?
- python-2.x - 需要程序重复而不是结束
- angular - 为什么 RxJs 在链式 http 调用完成之前完成执行?