简体   繁体   中英

Tensorflow model not training in javascript

TensorFlow.js version

1.4.0.

Description

I am trying to train a model in javascript and the model fails to learn or converge. I took the same model and the same data, which I used in the python version of the program, so I expected the model to learn on the same phase. On the contrary, the model fails to improve and the validation accuracy stays the same after the first run. The python model is able to achieve accuracy of ~70%, whereas the javascript model hardly achieves better than 5% after 50 epochs. The URLs work, if you want to work with the same data.

Code to reproduce the bug

Python code:

 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 code:

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

You can try this architecture also. I think this will help sometime improve your model accuracy:

async function* data() {
  while (true) {
    for (i in train) {
      // this function return tensor data
    }
  }
}
async function* labels() {
  while (true) {
    for (i in train) {
      // this function return tensor label
    }
  }
}

async function initModel() {
   // model write here
}


(async function () {

const xs = tf.data.generator(data);
const ys = tf.data.generator(labels);

const model = await initModel();
model.summary();

 await model.fit(xs, ys,{    
    epochs: 5
    batchesPerEpoch: 5 
  });
})()

Give your feedback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM