Tensorflow.js:Tensor在使用tanh或sigmoid时处理了错误,但没有使用relu激活函数

Tensorflow.js:Tensor在使用tanh或sigmoid时处理了错误,但没有使用relu激活函数,tensorflow,neural-network,tensorflow.js,activation-function,Tensorflow,Neural Network,Tensorflow.js,Activation Function,在TensorFlow.js中,我创建了一个具有3个密集层的顺序神经网络,当我将激活函数设置为“relu”时,该网络会工作,但当我尝试“tanh”或“sigmoid”时,它会抛出错误,“error:Tensor is disposed” 我做了一个模型总结,以验证更改激活函数不会改变网络结构或参数编号。我还试着注释了我正在使用的tf.tidy 这是我的模型: constmymodel=tf.sequential(); add(tf.layers.dense({units:64,inputShap

在TensorFlow.js中,我创建了一个具有3个密集层的顺序神经网络,当我将激活函数设置为“relu”时,该网络会工作,但当我尝试“tanh”或“sigmoid”时,它会抛出错误,“error:Tensor is disposed”

我做了一个模型总结,以验证更改激活函数不会改变网络结构或参数编号。我还试着注释了我正在使用的
tf.tidy

这是我的模型:

constmymodel=tf.sequential();
add(tf.layers.dense({units:64,inputShape:[1],activation:'tanh'}));
add(tf.layers.dense({units:64,inputShape:[1],激活:'relu'}));
add(tf.layers.densite({units:1}));
'tanh'
切换到
'relu'
可以解决问题,但我不知道为什么

这是我的培训代码:

优化器。最小化(()=>{
设输入=tf.tensor2d(x_vals);
让预测=myModel.PredictionBatch(输入);
设totaloss=tf.损失.均方误差(tf.张量2d(y_vals),预测);
返回总OSS;
});
完整代码段(运行一秒钟):

x\u vals=[
[1],
[2],
[3],
[4],
[5]
];
y_vals=[
[1],
[2],
[3],
[4],
[5]
];
常量优化器=tf.train.adam(.005);
const myModel=tf.sequential();
add(tf.layers.dense({units:64,inputShape:[1],activation:'tanh'}));
add(tf.layers.dense({units:64,activation:'relu'}));
add(tf.layers.densite({units:1}));
myModel.summary();
优化程序。最小化(()=>{
设输入=tf.tensor2d(x_vals);
让预测=myModel.PredictionBatch(输入);
设totaloss=tf.损失.均方误差(tf.张量2d(y_vals),预测);
返回总OSS;
});
曲线=[];
for(设i=0;i

该问题与激活层无关。很可能您正在使用的张量已经在
tf.tidy
回调中释放

下面是一个使用
tanh
sigmoid
激活层的简单序列模型

const model = tf.sequential({
    layers: [
      tf.layers.dense({ units: 64, inputShape: [1], activation: 'tanh' }),
      tf.layers.dense({ units: 64, inputShape: [1], activation: 'sigmoid' }),
      tf.layers.dense({ units: 1 })
    ]
});
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
for (let i = 1; i < 5 ; ++i) {
  const h = await model.fit(tf.ones([8, 1]), tf.ones([8, 1]), {
      batchSize: 4,
      epochs: 3
  });
  console.log("Loss after Epoch " + i + " : " + h.history.loss[0]);
}
const model=tf.sequential({
图层:[
密集({units:64,inputShape:[1],激活:'tanh'}),
密集({units:64,inputShape:[1],激活:'sigmoid'}),
密度({units:1})
]
});
compile({optimizer:'sgd',loss:'meanSquaredError'});
for(设i=1;i<5;++i){
常数h=等待模型拟合(tf.ones([8,1]),tf.ones([8,1]){
批量大小:4,
时代:3
});
console.log(“历元后的损失”+i+:“+h.history.Loss[0]);
}

我从代码中删除了所有
tf.tidy
回调和
dispose
调用,但我仍然遇到同样的问题。有趣的是,当我使用
model.fit
而不是
optimizer.minimize
时,这个问题就消失了。我提供了一个完整的代码片段来运行,也许这将有助于澄清。我想知道是否应该将此作为一个bug发布在Tensorflow.js Github页面上。