Comments (4)
So, it does look like tf.layers.activation
should work here, as a replacement to the string activations. I hadn't noticed this layer type before:
https://js.tensorflow.org/api/latest/#layers.activation
The other stuff is still a problem, so far as I can see.
from tfjs.
You haven't registered the weights from the child layers to the parent later In the build method.
this.trainableWeights = [...this.childLayer.trainableWeights]
Same goes for nonTrainableWeights
We really need some documentation written up for the layers API, especially the rnncell base class
from tfjs.
Thanks for the suggestion, @lukemovement. Your suggestion does work, however you also need to build each of the child layers:
this.inProj.build(inputShape)
this.outProj.build(inputShape)
I actually discovered this trick previously, and it also has problems. The issue relates to restoring a model from disk:
/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:273
var _this = _super.call(this, message) || this;
^
ValueError: Duplicate weight name: glu-TLd/kernel
at new ValueError (/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:273:28)
at Container.loadWeights (/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:21823:35)
at /home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:25792:27
at step (/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:159:27)
at Object.next (/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:108:53)
at fulfilled (/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:89:28)
Node.js v18.18.2
When you add child layers to trainableWeights
like this, every child layer will inherit the name of its parent (with a suffix):
LayerVariable {
dtype: 'float32',
shape: [ 333 ],
id: 30,
originalName: 'glu-FpC/bias',
name: 'glu-FpC/bias_2',
trainable_: true,
constraint: null,
val: Variable {
kept: false,
isDisposedInternal: false,
shape: [Array],
dtype: 'float32',
size: 333,
strides: [],
dataId: {},
id: 49,
rankType: '1',
trainable: true,
name: 'glu-FpC/bias_2'
}
}
The end result causes issues when attempting to restore those child layers from a checkpoint. I thought it might be possible to fix this with the getWeights()
and setWeights()
method, but couldn't find an immediate solution:
getWeights() {
return this.trainableWeights.map((weights) => weights.read())
}
setWeights(weights) {
this.inProj.kernel.write(weights[0])
this.inProj.bias.write(weights[1])
this.outProj.kernel.write(weights[2])
this.outProj.bias.write(weights[3])
}
Anyway, I hope that additional context helps.
If you can point me to somewhere I could contribute to the docs, I might be able to write a tutorial or something. I've probably built 100 custom layers at this point.
from tfjs.
Thanks for the suggestion, @lukemovement. Your suggestion does work, however you also need to build each of the child layers:
this.inProj.build(inputShape) this.outProj.build(inputShape)I actually discovered this trick previously, and it also has problems. The issue relates to restoring a model from disk:
/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:273 var _this = _super.call(this, message) || this; ^ ValueError: Duplicate weight name: glu-TLd/kernel at new ValueError (/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:273:28) at Container.loadWeights (/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:21823:35) at /home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:25792:27 at step (/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:159:27) at Object.next (/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:108:53) at fulfilled (/home/crow/Repos/ode/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:89:28) Node.js v18.18.2
When you add child layers to
trainableWeights
like this, every child layer will inherit the name of its parent (with a suffix):LayerVariable { dtype: 'float32', shape: [ 333 ], id: 30, originalName: 'glu-FpC/bias', name: 'glu-FpC/bias_2', trainable_: true, constraint: null, val: Variable { kept: false, isDisposedInternal: false, shape: [Array], dtype: 'float32', size: 333, strides: [], dataId: {}, id: 49, rankType: '1', trainable: true, name: 'glu-FpC/bias_2' } }
The end result causes issues when attempting to restore those child layers from a checkpoint. I thought it might be possible to fix this with the
getWeights()
andsetWeights()
method, but couldn't find an immediate solution:getWeights() { return this.trainableWeights.map((weights) => weights.read()) } setWeights(weights) { this.inProj.kernel.write(weights[0]) this.inProj.bias.write(weights[1]) this.outProj.kernel.write(weights[2]) this.outProj.bias.write(weights[3]) }Anyway, I hope that additional context helps.
If you can point me to somewhere I could contribute to the docs, I might be able to write a tutorial or something. I've probably built 100 custom layers at this point.
I'm unsure as to where this would best be placed. The weights are always in the same order on the layer so I use this. It works as long as you don't reach the maximum string length.
import * as tf from "@tensorflow/tfjs";
import { mkdir, readFile, writeFile } from "fs/promises";
import { resolve } from "path";
export const SaveModel = async ({
model,
dir,
}: {
model: tf.LayersModel;
dir: string;
}) => {
for (const layer of model.layers) {
const name = layer.name;
if (0 === layer.weights.length) {
continue;
}
const weights = JSON.stringify(
layer.getWeights().map((weight) => weight.arraySync()),
);
await mkdir(dir, { recursive: true });
await writeFile(resolve(dir, `${name}.json`), weights);
}
console.log(`Saved to ${dir}`);
};
export const LoadModel = async ({
model,
dir,
}: {
model: tf.LayersModel;
dir: string;
}) => {
for (const layer of model.layers) {
const name = layer.name;
if (0 === layer.weights.length) {
continue;
}
try {
const weights = JSON.parse(
await readFile(resolve(dir, `${name}.json`), "utf-8"),
);
const tensors = weights.map((weight: number[]) => tf.tensor(weight));
layer.setWeights(tensors);
} catch (e) {
console.log(layer.name, (e as Error).message);
}
}
console.warn(`Loaded from ${dir}`);
};
from tfjs.
Related Issues (20)
- tfjs-node - upgrade tar >=6.2.1 HOT 3
- Undefined variable tensorflow-library-target in binding.gyp HOT 4
- tensorflowjs_converter: NotImplementedError: numpy() is only available when eager execution is enabled HOT 9
- tensorflowjs_converter on .keras file messes up weights HOT 6
- Safari webworker `Error when getting WebGL Context` HOT 4
- tfjs-node-gpu warnings (not running in GPU mode) HOT 2
- Model with softmax output layer not outputing probabilities but just 1 and 0 HOT 4
- Model Face Detection Confidence Score Output or Minimum Confidence Score Threshold
- model.executeAsync fails when loading multiple converted TF-DF models HOT 4
- TensorFlowJS fails reliably on Intel GPU running Windows, otherwise succeeds HOT 1
- Error when a module try to install tensorflow HOT 4
- I can't install @tensorflow/tfjs-node in my project HOT 5
- Finish placeholder features: einsum gradients, conv1d causal padding HOT 3
- Tfjs pose detection not working with newer Android devices (12, 13, 14) HOT 7
- Can you guide on how to load model (model.json and group1-shard1of1.bin files) which are downloaded to Documents Directory in iOS and internal app storage folder in Android ? HOT 3
- npm install @tensorflow/tfjs-node error HOT 4
- Cannot import TFJS-Node HOT 5
- memory leak (core dumped) problem in tfjs-node HOT 2
- Error: Kernel 'Draw' not registered for backend 'custom-webgl2' at Engine.runKernel at Module.draw HOT 5
- Face Detection not defined HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tfjs.