首页 > 解决方案 > 想要使用 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()

标签: pythontensorflow

解决方案


首先加载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)

推荐阅读