This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[67 46 44 65] | |
[67 46 44 65] | |
[67 35 11 0] | |
[67 35 11 0] | |
[67 35 11 0] | |
[67 46 44 65] | |
[67 46 44 65] | |
[67 35 11 0]] | |
**************************************** | |
[[67 67] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
tf.enable_eager_execution() | |
file_name = 'sentiment.csv' | |
batch_size = 8 | |
num_buckets = 4 | |
output_buffer_size = batch_size * 1000 | |
reshuffle_each_iteration = True | |
random_seed = 1023 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __call__(self, inputs): | |
emb = self.embed(inputs) | |
d1 = self.dense1(emb) | |
d1 = self.global_pooling(d1) | |
outputs = self.pred(d1) | |
return outputs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.global_pooling = tf.keras.layers.GlobalAveragePooling1D() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.dense1 = tf.keras.layers.Dense(units=hid_size, activation=tf.nn.relu) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.embed = tf.keras.layers.Embedding(vocab_size, 128) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
table = tf.contrib.lookup.index_table_from_file(vocabulary_file="vocab.txt", num_oov_buckets=1) | |
csv_dataset = csv_dataset.map(lambda x, y: (tf.string_split([x]).values, y)) | |
csv_dataset = csv_dataset.map(lambda x, y: (tf.cast(table.lookup(x), tf.int32), y)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
csv_dataset = csv_dataset.map(lambda x, y: (x, tf.one_hot(y, 2))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# convert string labels into numeric value | |
csv_dataset = csv_dataset.map(lambda x, y: (x, tf.cond(tf.equal(y, 'positive'), lambda: 1, lambda: 0))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# change label-sentence to sentence-label | |
csv_dataset = csv_dataset.map(lambda x, y: (y, x)) |