简体   繁体   English

Tensorflow,从 csv 文件读取数据时出现 Conv1D 错误

[英]Tensorflow, Conv1D error with reading data from csv file

I am a newbie at TensorFlow and try to set up the prediction model for graduate school acceptance.我是 TensorFlow 的新手,并尝试为研究生院录取设置预测 model。

By using pandas, I was able to prepare the training data set as a list.通过使用 pandas,我能够将训练数据集准备为列表。

The data of the CSV file are composed of 4 columns and 425 rows (the first column (admit) is the answer, 3 more columns (gre, gpa, rank) are the training data) as shown below. CSV文件的数据由4列425行组成(第一列(admit)是答案,另外3列(gre、gpa、rank)是训练数据)如下图所示。

在此处输入图像描述

import pandas as pd

data = pd.read_csv('gpascore.csv')
data = data.dropna()

data_y = data['admit'].values
data_x = []

for i, rows in data.iterrows():
   data_x.append([rows['gre'], rows['gpa'], rows['rank']])

And I tried to revise a prediction model by adding 1D convolution layer and maxpooling layer as below.我尝试通过添加一维卷积层和最大池化层来修改预测 model,如下所示。 Originally, the prediction model includes only Dense layers.最初,预测 model 仅包括密集层。

import numpy as np    
import tensorflow as tf

model = tf.keras.models.Sequential([
   tf.keras.layers.Conv1D(32, 3, activation='relu', input_shape=(3)),
   tf.keras.layers.MaxPooling1D(1),
   tf.keras.layers.Flatten(),
   tf.keras.layers.Dense(64, activation='tanh'),   
   tf.keras.layers.Dense(1, activation='sigmoid'),
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(np.array(data_x), np.array(data_y), epochs=100)

score = model.evaluate(data_x, data_y)
print(score)

pred = model.predict([ [750, 3.70, 3], [400, 2.2, 1] ])
print(pred)

However, I have some errors as shown below.但是,我有一些错误,如下所示。

在此处输入图像描述

And I tried to change the batch size and kernal size, but I still have the same errors.我试图改变批量大小和内核大小,但我仍然有同样的错误。

Could someone help me to correct the errors, please?有人可以帮我纠正错误吗?

Thank you谢谢

================================================================= ==================================================== ================

I really appreciate the help.我真的很感激帮助。 And I updated the new errors below.我更新了下面的新错误。

在此处输入图像描述

在此处输入图像描述

Problem with the COnv1D line COnv1D 线的问题

tf.keras.layers.Conv1D(32, 3, activation='relu', input_shape=(3))

Conv1D over sequences expects 3dimension input.序列上的 Conv1D 需要 3 维输入。 [batch, time_steps, single_vector] Add extra dimension to your input. [batch, time_steps, single_vector]为您的输入添加额外的维度。 First convert dataframe to numpy_array首先将 dataframe 转换为 numpy_array

data_x = data_x.to_numpy()
data_x = data_x.reshape(-1,425,3)

model = tf.keras.models.Sequential([
   tf.keras.layers.Conv1D(32, 3, activation='relu', input_shape=[None,3]),
   tf.keras.layers.MaxPooling1D(1),
   tf.keras.layers.Flatten(),
   tf.keras.layers.Dense(64, activation='tanh'),   
   tf.keras.layers.Dense(1, activation='sigmoid'),
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X, np.array(data_y), epochs=100)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM