我正在使用自己的数据集,我想使用tflearn进行深度神经网络。
这是我的代码的一部分。
import tflearn
from tflearn.data_utils import load_csv
#Load the CSV File
X, Y = load_csv('data.csv')
#Split Data in train and Test with tflearn
¿我怎么能在TFLearn中执行一项功能来分割X,Y并获得train_X,test_X,train_Y,test_Y?
我知道如何处理numpy和其他库,但我想使用tflearn。
答案 0 :(得分:1)
在tflearn(http://tflearn.org/models/dnn/)中fit
模型的tflearn.DNN
方法中,您可以将选项validation_set
设置为小于1的浮点数,然后设置模型在培训期间,它会自动将您的输入分成培训和验证集。
import tflearn
from tflearn.data_utils import load_csv
#Load the CSV File
X, Y = load_csv('data.csv')
# Define some network
network = ...
# Training
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=20, validation_set=0.1) # will use 10% for validation
这将在训练时创建验证集,这与测试集不同。如果您只想要一个火车和测试装置,我建议您查看sklearn的train_test_split功能,该功能也可以为您分割数据。
答案 1 :(得分:0)
Nicki的答案是我认为最简单的解决方案。
但另一个简单的解决方案是使用sklearn和train_test_split()
from sklearn.model_selection import train_test_split
data, target = load_raw_data(data_size) # own method, data := ['hello','...'] target := [1 0 -1] label
X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.33, random_state=42)
或numpy版本:
import numpy as np
texts, target = load_raw_data(data_size) # own method, texts := ['hello','...'] target := [1 0 -1] label
train_indices = np.random.choice(len(target), round(0.8 * len(target)), replace=False)
test_indices = np.array(list(set(range(len(target))) - set(train_indices)))
x_train = [x for ix, x in enumerate(texts) if ix in train_indices]
x_test = [x for ix, x in enumerate(texts) if ix in test_indices]
y_train = np.array([x for ix, x in enumerate(target) if ix in train_indices])
y_test = np.array([x for ix, x in enumerate(target) if ix in test_indices])
所以这是你的选择,快乐的编码:)