class CNN extends tf.layers.Layer { constructor(NUMOUTPUTCLASSES = 10) { super({});…
- User
class CNN extends tf.layers.Layer { constructor(NUM_OUTPUT_CLASSES = 10) { super({}); this.conv2d_1 = tf.layers.conv2d({ inputShape: [28, 28, 1], kernelSize: 5, filters: 8, strides: 1, activation: 'relu', kernelInitializer: 'varianceScaling' }) this.maxPool_1 = tf.layers.maxPooling2d({poolSize: [2, 2], strides: [2, 2]}) this.conv2d_2 = tf.layers.conv2d({ kernelSize: 5, filters: 16, strides: 1, activation: 'relu', kernelInitializer: 'varianceScaling' })
codethis.maxPool_2 = tf.layers.maxPooling2d({poolSize: [2, 2], strides: [2, 2]}) this.flatten = tf.layers.flatten() this.dense = tf.layers.dense({ units: 10, kernelInitializer: 'varianceScaling', activation: 'softmax' }) } call(x, kwargs) { x = this.conv2d_1.apply(x) x = this.maxPool_1.apply(x) x = this.conv2d_2.apply(x) x = this.maxPool_2.apply(x) x = this.flatten.apply(x) x = this.dense.apply(x) return x } getClassName() { return 'CNN' } }async function train(ds) { const model = new CNN() // model.fitDataset(ds, {epochs: 5}).then(info => { // console.log('Accuracy', info.history.acc); // });
const optimizer = tf.train.adam(0.001); for (let epoch = 0; epoch < 5; epoch++) { await ds.forEachAsync(({xs, ys}) => { optimizer.minimize(() => { const predYs = model.call(xs); const loss = tf.losses.softmaxCrossEntropy(ys, predYs); loss.data().then(l => console.log('Loss', l[0])); return loss; }); }); console.log('Epoch', epoch); } }
function* data() { for (let i = 0; i < 1; i++) { // Generate one sample at a time. yield tf.randomNormal([28, 28, 1]); } }
codefunction* labels() { for (let i = 0; i < 1; i++) { // Generate one sample at a time. yield tf.randomUniform([10]); } }const xs = tf.data.generator(data); const ys = tf.data.generator(labels); const ds = tf.data.zip({xs, ys}).shuffle(1).batch(1024);
train(ds) Error: Cannot find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize().
- Assistant
It looks like you are encountering an error due to TensorFlow.js not being able to find a connection between variables and the loss function. One possible reason for this error is the use of the
callmethod directly, which may break the connection between the variables and the loss function.Instead, you should use the
applymethod to apply the model to the input tensor. Here is the modifiedtrainfunction that uses theapplymethod:javascriptasync function train(ds) { const model = new CNN(); const optimizer = tf.train.adam(0.001); for (let epoch = 0; epoch < 5; epoch++) { await ds.forEachAsync(({xs, ys}) => { optimizer.minimize(() => { const predYs = model.apply(xs); // Use apply instead of call const loss = tf.losses.softmaxCrossEntropy(ys, predYs); loss.data().then(l => console.log('Loss', l[0])); return loss; }); }); console.log('Epoch', epoch); } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.