MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / buildModel

Function buildModel

sentiment/train.js:36–85  ·  view source on GitHub ↗
(modelType, maxLen, vocabularySize, embeddingSize)

Source from the content-addressed store, hash-verified

34 * @returns An uncompiled instance of `tf.Model`.
35 */
36export function buildModel(modelType, maxLen, vocabularySize, embeddingSize) {
37 // TODO(cais): Bidirectional and dense-only.
38 const model = tf.sequential();
39 if (modelType === 'multihot') {
40 // A 'multihot' model takes a multi-hot encoding of all words in the
41 // sentence and uses dense layers with relu and sigmoid activation functions
42 // to classify the sentence.
43 model.add(tf.layers.dense({
44 units: 16,
45 activation: 'relu',
46 inputShape: [vocabularySize]
47 }));
48 model.add(tf.layers.dense({
49 units: 16,
50 activation: 'relu'
51 }));
52 } else {
53 // All other model types use word embedding.
54 model.add(tf.layers.embedding({
55 inputDim: vocabularySize,
56 outputDim: embeddingSize,
57 inputLength: maxLen
58 }));
59 if (modelType === 'flatten') {
60 model.add(tf.layers.flatten());
61 } else if (modelType === 'cnn') {
62 model.add(tf.layers.dropout({rate: 0.5}));
63 model.add(tf.layers.conv1d({
64 filters: 250,
65 kernelSize: 5,
66 strides: 1,
67 padding: 'valid',
68 activation: 'relu'
69 }));
70 model.add(tf.layers.globalMaxPool1d({}));
71 model.add(tf.layers.dense({units: 250, activation: 'relu'}));
72 } else if (modelType === 'simpleRNN') {
73 model.add(tf.layers.simpleRNN({units: 32}));
74 } else if (modelType === 'lstm') {
75 model.add(tf.layers.lstm({units: 32}));
76 } else if (modelType === 'bidirectionalLSTM') {
77 model.add(tf.layers.bidirectional(
78 {layer: tf.layers.lstm({units: 32}), mergeMode: 'concat'}));
79 } else {
80 throw new Error(`Unsupported model type: ${modelType}`);
81 }
82 }
83 model.add(tf.layers.dense({units: 1, activation: 'sigmoid'}));
84 return model;
85}
86
87function parseArguments() {
88 const parser = new ArgumentParser(

Callers 3

embedding_test.jsFile · 0.90
train_test.jsFile · 0.90
mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected