1.4.0。
描述我正在尝试使用javascript训练模型,但是该模型无法学习或收敛。我采用了该程序的python版本中使用的相同模型和相同数据,因此我希望该模型可以在同一阶段学习。相反,该模型无法改进,并且首次运行后验证准确性保持不变。 python模型能够达到约70%的准确性,而javascript模型在50个历元后几乎无法达到5%以上。如果您要使用相同的数据,则可以使用URL。
重现该错误的代码Python代码:
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);
答案 0 :(得分:0)
您也可以尝试这种体系结构。我认为这将有助于提高模型的准确性:
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
});
})()
提供您的反馈意见。