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
| mnist = tf.keras.datasets.mnist | |
| (x_train, y_train), (x_test, y_test) = mnist.load_data() | |
| # some reshaping here... | |
| X = tf.placeholder(tf.float32, [None, 784], name="X") | |
| Y_truth = tf.placeholder(tf.float32, [None, 10], name="Y") | |
| # define model, loss, and optimizer |
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
| dataset = tf.data.TextLineDataset("file.txt") | |
| for line in dataset: | |
| print(line) |
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
| > TypeError: 'TextLineDataset' object is not iterable | |
| or | |
| > RuntimeError: dataset.__iter__() is only supported when eager execution is enabled. |
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
| print(type(dataset)) | |
| >>> <class 'tensorflow.python.data.ops.readers.TextLineDataset'> |
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 | |
| dataset = tf.data.TextLineDataset("file.txt") | |
| iterator = dataset.make_one_shot_iterator() | |
| next_element = iterator.get_next() | |
| with tf.Session() as sess: | |
| print(sess.run(next_element)) | |
| print(sess.run(next_element)) | |
| print(sess.run(next_element)) |
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 | |
| dataset = tf.data.TextLineDataset("file.txt") | |
| iterator = dataset.make_initializable_iterator() | |
| next_element = iterator.get_next() | |
| init_op = iterator.initializer | |
| with tf.Session() as sess: | |
| # Initialize the iterator | |
| sess.run(init_op) |
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 time | |
| import numpy as np | |
| import tensorflow as tf | |
| # Step 1: Read in data | |
| (train_x, train_y), (test_x, test_y) = tf.keras.datasets.mnist.load_data() | |
| m_train = train_x.shape[0] | |
| m_test = test_x.shape[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
| import time | |
| import numpy as np | |
| import tensorflow as tf | |
| # Step 1: Read in data | |
| (train_x, train_y), (test_x, test_y) = tf.keras.datasets.mnist.load_data() | |
| m_train = train_x.shape[0] | |
| m_test = test_x.shape[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
| # create training Dataset and batch it | |
| train_data = tf.data.Dataset.from_tensor_slices(train) | |
| train_data = train_data.shuffle(10000) # if you want to shuffle your data | |
| train_data = train_data.batch(128) | |
| # create testing Dataset and batch it | |
| test_data = tf.data.Dataset.from_tensor_slices(test) | |
| test_data = test_data.shuffle(10000) | |
| test_data = test_data.batch(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
| # create weights and bias for logistic regression | |
| # w is initialized to random variables with mean of 0, stddev of 0.01 | |
| # b is initialized to 0 | |
| # shape of w depends on the dimension of X and Y so that Y = tf.matmul(X, w) | |
| # shape of b depends on Y | |
| w = tf.get_variable(name='weight', | |
| initializer=tf.truncated_normal(shape=[784, 10], mean=0, stddev=0.01)) | |
| b = tf.get_variable(name='bias', initializer=tf.zeros([10])) |