我有两个预测标签的值,-1或1。使用 LSTM
或 Dense
进行学习效果良好,但对于不同的预测数据集,预测结果总是相同的,将层更改为 Dense 不会改变预测结果,也许我做错了什么。
以下是代码:
// set up data arrays
float[,,] training_data = new float[training.Count(), 12, 200];
float[,,] testing_data = new float[testing.Count(), 12, 200];
float[,,] predict_data = new float[1, 12, 200];
IList<float> training_labels = new List<float>();
IList<float> testing_labels = new List<float>();
// Load Data and add to arrays
...
...
/////////////////////////
NDarray train_y = np.array(training_labels.ToArray());
NDarray train_x = np.array(training_data);
NDarray test_y = np.array(testing_labels.ToArray());
NDarray test_x = np.array(testing_data);
NDarray predict_x = np.array(predict_data);
train_y = Util.ToCategorical(train_y, 2);
test_y = Util.ToCategorical(test_y, 2);
//Build functional model
var model = new Sequential();
model.Add(new Input(shape: new Keras.Shape(12, 200)));
model.Add(new BatchNormalization());
model.Add(new LSTM(128, activation: "tanh", recurrent_activation: "sigmoid", return_sequences: false));
model.Add(new Dropout(0.2));
model.Add(new Dense(32, activation: "relu"));
model.Add(new Dense(2, activation: "softmax"));
model.Compile(optimizer: new SGD(), loss: "binary_crossentropy", metrics: new string[] { "accuracy" });
model.Summary();
var history = model.Fit(train_x, train_y, batch_size: 1, epochs: 1, verbose: 1, validation_data: new NDarray[] { test_x, test_y });
var score = model.Evaluate(test_x, test_y, verbose: 2);
Console.WriteLine($"Test loss: {score[0]}");
Console.WriteLine($"Test accuracy: {score[1]}");
NDarray predicted=model.Predict(predict_x, verbose: 2);
Console.WriteLine($"Prediction: {predicted[0][0]*100}");
Console.WriteLine($"Prediction: {predicted[0][1]*100}");
这是输出结果
483/483 [==============================]
- 9s 6ms/step - loss: 0.1989 - accuracy: 0.9633 - val_loss: 0.0416 - val_accuracy: 1.0000
4/4 - 0s - loss: 0.0416 - accuracy: 1.0000
Test loss: 0.04155446216464043
Test accuracy: 1
1/1 - 0s
Prediction: 0.0010418787496746518
Prediction: 99.99896287918091
在使用ML.Net时,相同的预测数据会产生不同的结果,但准确度仅为0.6,因此我需要深度学习。
[:5]
被称为 Python 中的数组切片。这对你来说不是很重要。我用它来进行检查目的。例如,当我使用print(pred[:5])
时,它意味着打印出前 5 个元素或者前 5 个样本的概率分数。 - Innat