Javascript 通用Tensorflow.js培训示例
我正在训练神经网络做一些图像处理。我用Synaptic.js成功地做到了这一点,但当我不得不使用更多层时,它的学习速度非常慢。js示例描述了一些特定的案例,很难理解它们并应用到我的案例中。有人能帮我把Synaptic.js代码转换成Tensorflow.js吗?输入是RGB像素[0..1]的3x3(或更多)内核,输出是单个RGB像素[0..1]Javascript 通用Tensorflow.js培训示例,javascript,tensorflow.js,synaptic.js,Javascript,Tensorflow.js,Synaptic.js,我正在训练神经网络做一些图像处理。我用Synaptic.js成功地做到了这一点,但当我不得不使用更多层时,它的学习速度非常慢。js示例描述了一些特定的案例,很难理解它们并应用到我的案例中。有人能帮我把Synaptic.js代码转换成Tensorflow.js吗?输入是RGB像素[0..1]的3x3(或更多)内核,输出是单个RGB像素[0..1] const layers = [27, 9, 3]; const learningRate = 0.05; const perceptron = new
const layers = [27, 9, 3];
const learningRate = 0.05;
const perceptron = new Synaptic.Architect.Perceptron(layers);
// Train
sampleData.forEach(([input, output]) => {
perceptron.activate(input);
perceptron.propagate(learningRate, output);
});
// Get result
const result = realData.map((input) => perceptron.activate(input));
示例repo中有一些非常通用的TensorFlow.js示例: 对于您的情况,您需要在回购协议中执行类似iris的操作
// Define the model.
const model = tf.sequential();
// you will need to provide the size of the individual inputs below
model.add(tf.layers.dense({units: 27, inputShape: INPUT_SHAPE}));
model.add(tf.layers.dense({units: 9});
model.add(tf.layers.dense({units: 3});
const optimizer = tf.train.adam(0.05);
modcel.compile({
optimizer: optimizer,
loss: 'categoricalCrossentropy',
metrics: ['accuracy'],
});
// Train.
const lossValues = [];
const accuracyValues = [];
// Call `model.fit` to train the model.
const history = await model.fit(input, output, {epochs: 10});
// Get result
const result = realData.map((input) => model.predict(input));
示例repo中有一些非常通用的TensorFlow.js示例: 对于您的情况,您需要在回购协议中执行类似iris的操作
// Define the model.
const model = tf.sequential();
// you will need to provide the size of the individual inputs below
model.add(tf.layers.dense({units: 27, inputShape: INPUT_SHAPE}));
model.add(tf.layers.dense({units: 9});
model.add(tf.layers.dense({units: 3});
const optimizer = tf.train.adam(0.05);
modcel.compile({
optimizer: optimizer,
loss: 'categoricalCrossentropy',
metrics: ['accuracy'],
});
// Train.
const lossValues = [];
const accuracyValues = [];
// Call `model.fit` to train the model.
const history = await model.fit(input, output, {epochs: 10});
// Get result
const result = realData.map((input) => model.predict(input));
这看起来很简单,但我在选择张量的形状时遇到了问题。实际上,我希望我的输入是[3,3,3](3x3像素),输出是[3](单像素),所以输入层应该有27个神经元,输出层应该有3个神经元。我想训练它通过100个像素,所以我设置了
inputShape:[100,3,3]
并创建输入[100,3,3]和输出[100,3]张量。但它失败了,错误是它期望5D数组作为输入。最后我将输入展平为2D,它成功了。我不确定它是做什么的,等待结果。data()
返回一些疯狂的数字(例如,预期为[0..1],但得到了-170),但感谢跳转开始。将丢失替换为meanSquaredError
,这看起来很简单,但我在选择张量形状方面有问题。实际上,我希望我的输入是[3,3,3](3x3像素),输出是[3](单像素),所以输入层应该有27个神经元,输出层应该有3个神经元。我想训练它通过100个像素,所以我设置了inputShape:[100,3,3]
并创建输入[100,3,3]和输出[100,3]张量。但它失败了,错误是它期望5D数组作为输入。最后我将输入展平为2D,它成功了。我不确定它是做什么的,等待结果。data()
返回一些疯狂的数字(例如,预期为[0..1],但得到了-170),但感谢跳转开始。将丢失替换为meanSquaredError