本文整理汇总了TypeScript中@tensorflow/tfjs.layers.dense方法的典型用法代码示例。如果您正苦于以下问题:TypeScript layers.dense方法的具体用法?TypeScript layers.dense怎么用?TypeScript layers.dense使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@tensorflow/tfjs.layers
的用法示例。
在下文中一共展示了layers.dense方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: constructor
constructor() {
super('strike-zone');
this.fields = [
{key : 'px', min : PX_MIN, max : PX_MAX},
{key : 'pz', min : PZ_MIN, max : PZ_MAX},
{key : 'sz_top', min : SZ_TOP_MIN, max : SZ_TOP_MAX},
{key : 'sz_bot', min : SZ_BOT_MIN, max : SZ_BOT_MAX},
{key : 'left_handed_batter'}
];
this.data =
new PitchData('dist/strike_zone_training_data.json', 50, this.fields, 2,
(pitch) => pitch.type === 'S' ? 0 : 1);
const model = tf.sequential();
model.add(tf.layers.dense({
units : 20,
activation : 'relu',
inputShape : [ this.fields.length ]
}));
model.add(tf.layers.dense({units : 10, activation : 'relu'}));
model.add(tf.layers.dense({units : 2, activation : 'softmax'}));
model.compile({
optimizer : tf.train.adam(),
loss : 'categoricalCrossentropy',
metrics : [ 'accuracy' ]
});
this.model = model;
}
示例2: perceptron
function perceptron(x: tf.SymbolicTensor, init: any): tf.SymbolicTensor {
const depth = 8
const width = 32
for (var i = 0; i < depth; ++i) {
x = tf.layers.dense({ units: width, kernelInitializer: init, activation: 'tanh' }).apply(x) as tf.SymbolicTensor
}
return x
}
示例3: constructor
constructor() {
super('pitch-type');
this.fields = [
{key: 'vx0', min: VX0_MIN, max: VX0_MAX},
{key: 'vy0', min: VY0_MIN, max: VY0_MAX},
{key: 'vz0', min: VZ0_MIN, max: VZ0_MAX},
{key: 'ax', min: AX_MIN, max: AX_MAX},
{key: 'ay', min: AY_MIN, max: AY_MAX},
{key: 'az', min: AZ_MIN, max: AZ_MAX},
{key: 'start_speed', min: START_SPEED_MIN, max: START_SPEED_MAX},
{key: 'left_handed_pitcher'}
];
this.data = new PitchData(
'dist/pitch_type_training_data.json', 100, this.fields, 7,
(pitch) => pitch.pitch_code);
const model = tf.sequential();
model.add(tf.layers.dense(
{units: 250, activation: 'relu', inputShape: [this.fields.length]}));
model.add(tf.layers.dense({units: 175, activation: 'relu'}));
model.add(tf.layers.dense({units: 150, activation: 'relu'}));
model.add(tf.layers.dense({units: 7, activation: 'softmax'}));
model.compile({
optimizer: tf.train.adam(),
loss: 'categoricalCrossentropy',
metrics: ['accuracy']
});
this.model = model;
// All pitch data is stored sequentially (pitch_code 0-6) in the training
// files. Load the training and validation data file and glob all pitches of
// the same code together in batches. These will be used for calculating the
// class accuracy.
this.trainingClassTensors = concatPitchClassTensors(
'dist/pitch_type_training_data.json', this.fields, NUM_PITCH_CLASSES,
TRAINING_DATA_PITCH_CLASS_SIZE);
this.validationClassTensors = concatPitchClassTensors(
'dist/pitch_type_validation_data.json', this.fields, NUM_PITCH_CLASSES,
TRAINING_DATA_PITCH_CLASS_SIZE);
}
示例4: densenet
function densenet(x: tf.SymbolicTensor, init: any): tf.SymbolicTensor {
const depth = 8
const width = 8
for (var i = 0; i < depth; ++i) {
let y = tf.layers.dense({ units: width, kernelInitializer: init, activation: 'sigmoid' }).apply(x) as tf.SymbolicTensor
x = tf.layers.concatenate({}).apply([x, y]) as tf.SymbolicTensor
}
return x
}
示例5:
(async () => {
const model = tf.sequential({
layers: [
tf.layers.dense({ units: 200, inputShape: [400], activation: 'relu' }),
tf.layers.dense({ units: 200, activation: 'relu' }),
tf.layers.dense({ units: 200, activation: 'relu' }),
tf.layers.dense({ units: 1 }),
],
});
const optimizer = tf.train.sgd(0.001);
model.compile({ loss: 'meanSquaredError', optimizer });
const xs = tf.tensor2d(results.map(r => r.input));
const ys = tf.tensor2d(results.map(r => [r.fitness]));
const [evaluteX, trainX] = tf.split(xs, 2, 0);
const [evaluteY, trainY] = tf.split(ys, 2, 0);
await model
.fit(trainX, trainY, {
epochs: 100,
shuffle: true,
validationSplit: 0.2,
})
.then(h => {
console.log(h);
console.log(
`Learning Loss: ${h.history.loss[h.history.loss.length - 1]}`
);
console.log(
`Validation Loss: ${h.history.val_loss[h.history.val_loss.length - 1]}`
);
});
console.log(tf.memory());
})();
示例6: resnet
function resnet(x: tf.SymbolicTensor, init: any): tf.SymbolicTensor {
const depth = 8
const width = 32
for (var i = 0; i < depth; ++i) {
let y: tf.SymbolicTensor
if (i != 0)
y = tf.layers.activation({ activation: 'tanh' }).apply(x) as tf.SymbolicTensor
else
y = x
y = tf.layers.dense({ units: width, kernelInitializer: init, activation: 'tanh' }).apply(x) as tf.SymbolicTensor
let x_ = x
if (x_.shape[1] != width)
x_ = tf.layers.dense({ units: width, kernelInitializer: init }).apply(x_) as tf.SymbolicTensor
x = tf.layers.add().apply([x_, y]) as tf.SymbolicTensor
}
x = tf.layers.activation({ activation: 'tanh' }).apply(x) as tf.SymbolicTensor
return x
}
示例7: createModel
export function createModel(type: "densenet" | "perceptron" | "resnet", scale: number, bw = false, seed?: number): tf.Model {
let init = tf.initializers.varianceScaling({ scale: scale, mode: 'fanIn', distribution: 'normal', seed })
let binit = tf.initializers.zeros() // tf.initializers.randomNormal({ mean: 0, stddev: 4 })
let input = tf.input({ shape: [ 4 ] })
let x: tf.SymbolicTensor = input
switch (type) {
case "densenet":
x = densenet(x, init);
break;
case "perceptron":
x = perceptron(x, init);
break;
case "resnet":
x = resnet(x, init);
break;
}
x = tf.layers.dense({ units: bw ? 1 : 3, activation: 'tanh', kernelInitializer: tf.initializers.glorotNormal({ seed }) }).apply(x) as tf.SymbolicTensor
return tf.model({ inputs: input, outputs: x })
}
示例8: Scene
window.tf = tf;
const scene = new Scene();
const renderer = new Renderer(scene);
console.time('go!');
for (let i = 0; i < 60 * 10; i++) {}
console.timeEnd('go!');
console.log('constraints', scene.constraints.length);
console.log(scene.getPositions().length);
console.log(scene.fitness);
const model = tf.sequential({
layers: [
tf.layers.dense({ units: 32, inputShape: [24], activation: 'relu' }),
tf.layers.dense({ units: 10 }),
],
});
model.compile({ optimizer: 'sgd', loss: 'meanSquaredError' });
window.model = model;
const frame = function() {
scene.step();
tf.tidy(() => {
const output = model.predictOnBatch(
tf.tensor2d(scene.getPositions(), [1, 24])
) as tf.Tensor<tf.Rank.R1>;