我想使用tensorflow拆分csv的训练和测试数据,但我在张量中找不到像np.loadtxt这样的顺序,并尝试使用numpy进行拆分并将其转换为张量,但我得到了类似以下的错误:
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()发布于 2019-03-24 16:44:29
首先加载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)https://stackoverflow.com/questions/55321905
复制相似问题