javascript - python - tensorflow模型未在javascript中訓練

51 3
TensorFlow.js版本

1.4.0.

我嘗試在javascript中訓練模型,但模型無法學習到收斂,我採用python版本中使用的相同模型和相同數據,因此我希望模型可以在學習到同一階段,在首次運行后,模型無法提高,驗證精度保持不變,python模型能夠達到〜 70 %的準確度,而javascript模型在50個epoch后,無法達到5%以上的準確度。

重現bug的代碼


checkpoint = ModelCheckpoint('best_models/model--{val_accuracy:03f}--{epoch:03d}-{accuracy:03f}.h5', verbose=1, monitor='val_accuracy',save_best_only=True, mode='auto')


X_train_raw = requests.get("http://tb-test.chatbotech.com/info/get-training-arrays").json().get("xTrain")


X_test_raw = requests.get("http://tb-test.chatbotech.com/info/get-training-arrays").json().get("xTest")


y_train_raw = requests.get("http://tb-test.chatbotech.com/info/get-training-arrays").json().get("yTrain")


y_test_raw = requests.get("http://tb-test.chatbotech.com/info/get-training-arrays").json().get("yTest")


X_train = np.array(ast.literal_eval(X_train_raw))


X_test = np.array(ast.literal_eval(X_test_raw))


y_train_hot = np.array(ast.literal_eval(y_train_raw))


y_test_hot = np.array(ast.literal_eval(y_test_raw))



max_pad_length = 220



model = Sequential()


model.add(Conv2D(128, kernel_size=(8, 48), activation='relu', input_shape=(20, max_pad_length, 1)))


model.add(MaxPooling2D(pool_size=(3, 120)))


model.add(Dropout(0.2))


model.add(Dense(128, activation='relu'))


model.add(Dropout(0.3))


model.add(Flatten())


model.add(Dense(30, activation='softmax'))


model.compile(loss=keras.losses.categorical_crossentropy,


optimizer=keras.optimizers.Adadelta(),


metrics=['accuracy'])


history = model.fit(X_train, y_train_hot, batch_size=20, epochs=2000, verbose=1, validation_data=(X_test, y_test_hot),callbacks=[checkpoint])



Javascript代碼:


async function getData() {


 const dataReq = await fetch('http://tb-test.chatbotech.com/info/get-training-arrays');


 const trainData = await dataReq.json();


 return trainData;


}


async function run() {


 // Load and plot the original input data that we are going to train on.


 const data = await getData();


 console.log(data);


 const model = createModel();


 // More code will be added below


 model.fit(tf.tensor(JSON.parse(data.xTrain), [230, 20, 220, 1], 'float32'), tf.tensor(JSON.parse(data.yTrain), [230, 30]), { shuffle: false, epochs: 2000, validationData: [tf.tensor(JSON.parse(data.xTest), [154, 20, 220, 1], 'float32'), tf.tensor(JSON.parse(data.yTest), [154, 30])], callbacks: {


 async onEpochEnd(epoch, logs) {


 console.log(logs);


 },


 onBatchEnd(batch, logs) {


 console.log(logs);


 console.log(batch);


 }}});


}



function createModel() {


 const model = tf.sequential();


 model.add(tf.layers.conv2d({filters: 128, kernelSize: [8, 48], activation: 'relu', inputShape: [20, 220, 1], strides: [1, 1], padding: 'valid'}));


 model.add(tf.layers.maxPooling2d({poolSize: [3, 120], strides: [3, 120]}));


 model.add(tf.layers.dropout({rate: 0.2}));


 model.add(tf.layers.dense({units: 128, activation: 'relu'}));


 model.add(tf.layers.dropout({rate: 0.3}));


 model.add(tf.layers.flatten());


 model.add(tf.layers.dense({units: 30, activation: 'softmax'}));


 model.compile({loss: tf.metrics.categoricalCrossentropy, optimizer: tf.train.adadelta(1, 0.95, 1e-07 ), metrics: ['accuracy']});


 return model;


}


document.addEventListener('DOMContentLoaded', run);



时间: 原作者:

96 21

經過問題的再現之後,我認為Adadelta優化器在Python和TensorFlow.js之間的行為存在差異。

原作者:
...