Warm tip: This article is reproduced from serverfault.com, please click

python-如何将我的训练数据上传到Google中以进行Tensorflow Cloud训练

(python - How to upload my training data into google for Tensorflow cloud training)

发布于 2020-11-28 18:41:49

我想在gcp中训练我的keras模型。

我的代码:

这就是我加载数据集的方式

dataset = pandas.read_csv('USDJPY.fx5.csv', usecols=[2, 3, 4, 5], engine='python')

这就是我触发云训练的方式

job_labels = {"job": "forex-usdjpy", "team": "xxx", "user": "xxx"}
tfc.run(requirements_txt="./requirements.txt",
        job_labels=job_labels,
        stream_logs=True
        )

就在我的模型之前,应该没什么大不了的

model = Sequential()
model.add(LSTM(4, input_shape=(1, 4)))
model.add(Dropout(0.2))
model.add(Dense(4))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=1, batch_size=1, verbose=2)

一切正常,正在创建模型的docker映像,但未上传USDJPY.fx5.csv文件。所以我得到文件找不到错误

将自定义文件加载到培训工作中的正确方法是什么?我将火车数据上传到s3存储桶,但无法告诉Google去那里。

Questioner
Borislav Stoilov
Viewed
0
Borislav Stoilov 2020-12-19 00:21:57

原来是我的GCP配置有问题,这是我使其工作的步骤:

  • 创建一个s3存储桶,并将其中的所有文件公开,以便训练作业可以访问它们

  • 在要求fsspec和gcsfs中包括这两个

  • 像这样从panda.readCsv删除'engine'参数

    数据集= pandas.read_csv('gs:///USDJPY.fx5.csv',usecols = [2,3,4,5])

由于你是将python文件上传到GCP,因此是一种组织代码的好方法,可以将所有训练逻辑都放入一个方法中,然后在cloud train标志上有条件地调用它:

if tfc.remote():
    train()

如果有人感兴趣,这是完整的工作代码

import pandas
import numpy
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
from sklearn.preprocessing import MinMaxScaler
import tensorflow_cloud as tfc
import os

os.environ["PATH"] = os.environ["PATH"] + ":<path to google-cloud-sdk/bin"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "<path to google credentials json (you can generate this through their UI"


def create_dataset(data):
    dataX = data[0:len(data) - 1]
    dataY = data[1:]
    return numpy.array(dataX), numpy.array(dataY)

def train():
    dataset = pandas.read_csv('gs://<bucket>/USDJPY.fx5.csv', usecols=[2, 3, 4, 5])

    scaler = MinMaxScaler(feature_range=(-1, 1))
    scaler = scaler.fit(dataset)

    dataset = scaler.transform(dataset)

    # split into train and test sets
    train_size = int(len(dataset) * 0.67)
    train, test = dataset[0:train_size], dataset[train_size:len(dataset)]

    trainX, trainY = create_dataset(train)

    trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))

    model = Sequential()
    model.add(LSTM(4, input_shape=(1, 4)))
    model.add(Dropout(0.2))
    model.add(Dense(4))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.fit(trainX, trainY, epochs=1000, verbose=1)


job_labels = {"job": "forex-usdjpy", "team": "zver", "user": "zver1"}
tfc.run(requirements_txt="./requirements.txt",
        job_labels=job_labels,
        stream_logs=True
        )

if tfc.remote():
    train()

注意:这可能不是最佳的LSTM配置,请带一点盐