在TensorflowJS中构建神经网络

时间:2018-08-14 20:07:15

标签: python node.js tensorflow tensorflow.js

我使用nodejs和TensorflowJS构建了一个聊天机器人。

我的代码基于This tutorial

我无法“翻译”神经网络的构建。

发件人:

# Build neural network
net = tflearn.input_data(shape=[None, len(train_x[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(train_y[0]), activation='softmax')
net = tflearn.regression(net)

# Define model and setup tensorboard
model = tflearn.DNN(net, tensorboard_dir='tflearn_logs')
# Start training (apply gradient descent algorithm)
model.fit(train_x, train_y, n_epoch=1000, batch_size=8, show_metric=True)
model.save('model.tflearn') 

我到目前为止:

  // Build neural network:
  const model = tf.sequential();
  model.add(tf.layers.dense({units: training.length, activation: 'relu', inputShape: [train_x[0].length]}));
  model.add(tf.layers.dense({units: train_y[0].length, activation: 'linear'}));
  model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});

关键是我无法用自己的JS代码“预测”。

I got this erro

And this is my xs

完整代码https://github.com/ran-j/ChatBotNodeJS/blob/master/routes/index.js#L184

Expected dense_Dense1_input to have shape "a" but got array with shape "b"不同的是,因为它是python的'traduction',而不是使用create神经网络造成的错误

train_x[0].length = 48
train_y[0].length = 9

当我抓模型时,我会犯错。

1 个答案:

答案 0 :(得分:2)

尝试一下:

var model = tf.sequential();
model.add(tf.layers.dense({
    units: 8,
    inputShape: [null, train_x[0].length]
}));
model.add(tf.layers.dense({
    units: 8
}));
model.add(tf.layers.dense({
    units: train_y[0].length
    activation: 'softmax'
}));

model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

await model.fit(x_tensor, y_tensor, {
    epochs: 1000,
    batchSize: 8
})