我正在练习TF学习如何使用熊猫和泰坦尼克数据集。我认为我掌握了它,但在适应我的模型时,它崩溃了,我不知道为什么。我想这与我转换inputX and inputY
的方式有关,但我不确定,我认为这是正确的。
import numpy as np
import tensorflow as tf
import tflearn
import pandas as pd
# Download the Titanic dataset
from tflearn.datasets import titanic
titanic.download_dataset('titanic_dataset.csv')
from tflearn.data_utils import load_csv
dataframe = pd.read_csv('titanic_dataset.csv')
# okay lets drop the rest of the stuff from the table and keep those.
dataframe = dataframe.drop(["name", "ticket"], axis=1)
#lets change sex female/male to 1 and 0
dataframe['sex'].replace(['female','male'],[1,0],inplace=True)
# lets convert them so the tlearn can use doesnt crash
inputX = dataframe.loc[:, ['pclass', 'sex', 'age', 'sibsp', 'parch', 'fare']].as_matrix()
dataframe.loc[:, ("survived2")] = dataframe["survived"] == 0
dataframe.loc[:, ("survived2")] = dataframe["survived2"].astype(int)
inputY = dataframe.loc[:, ["survived", "survived2"]].as_matrix()
def NN():
net = tflearn.input_data(shape=[None, 6])
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 2, activation='softmax')
net = tflearn.regression(net)
# This model assumes that your network is named "net"
model = tflearn.DNN(net)
return model
# Define model
model = NN()
# Start training (apply gradient descent algorithm)
# Training
model.fit(inputX, inputY, validation_set=0.1, show_metric=True, batch_size=100, n_epoch=8)