javascript - python - tensorflow模型未在javascript中训练

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);



时间: 作者:

经过问题的再现之后,我认为Adadelta优化器在Python和TensorFlow.js之间的行为存在差异。

作者:
...