简体   繁体   English

Tensorflow 模型未在 javascript 中训练

[英]Tensorflow model not training in javascript

TensorFlow.js version TensorFlow.js 版本

1.4.0. 1.4.0。

Description 描述

I am trying to train a model in javascript and the model fails to learn or converge.我正在尝试用 javascript 训练模型,但该模型无法学习或收敛。 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.我在程序的python版本中使用了相同的模型和相同的数据,因此我希望模型在同一阶段学习。 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. python 模型能够达到约 70% 的准确率,而 javascript 模型在 50 个 epoch 后几乎没有达到超过 5%。 The URLs work, if you want to work with the same data.如果您想使用相同的数据,则 URL 有效。

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

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.提供您的反馈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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