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 json | |
| import torch | |
| from tokenizers import Tokenizer | |
| from transformers import ( | |
| AutoModelForCausalLM, | |
| AutoTokenizer, | |
| LlamaConfig, | |
| LlamaForCausalLM, | |
| ) |
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
| digit = 3 # The class to display average image of | |
| plt.imshow(x_train[np.argmax(y_train, axis=1) == digit].mean(axis=0).reshape(28,28), cmap='gray') |
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
| index = 18 | |
| print(np.argmax(y_train[index])) | |
| plt.imshow(x_train[index].reshape(28,28), cmap='gray') |
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
| BATCH_SIZE = 64 | |
| EPOCHS = 50 | |
| m = len(x_train) | |
| sess = tf.Session() | |
| sess.run(tf.global_variables_initializer()) |
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
| for epoch in range(EPOCHS): | |
| for i in range(BATCH_SIZE, m, BATCH_SIZE): | |
| x_batch = x_train[i-BATCH_SIZE:i] | |
| y_batch = y_train[i-BATCH_SIZE:i] | |
| sess.run(step, feed_dict={X:x_batch, labels:y_batch}) | |
| if (epoch+1) % 5 == 0: | |
| predictions, test_loss = sess.run([y, loss], feed_dict={X:x_test, | |
| labels:y_test}) | |
| accuracy = np.mean(np.argmax(y_test, axis=1) == np.argmax(predictions, | |
| axis=1)) |
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 | |
| from matplotlib import pyplot as plt | |
| import numpy as np | |
| from tensorflow.examples.tutorials.mnist import input_data | |
| mnist = input_data.read_data_sets('MNIST_data', one_hot=True) |
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
| # shuffle training data | |
| combined = list(zip(x_train, y_train)) | |
| np.random.shuffle(combined) | |
| x_train[:], y_train[:] = zip(*combined) | |
| # shuffle test data | |
| combined = list(zip(x_test, y_test)) | |
| np.random.shuffle(combined) | |
| x_test[:], y_test[:] = zip(*combined) |
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('x_train:', x_train.shape) | |
| print('y_train:', y_train.shape) | |
| print('x_test:', x_test.shape) | |
| print('y_test:', y_test.shape) |
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
| predictions = sess.run(y, feed_dict={X:x_test}) | |
| index = 9 | |
| print('Prediction: %d, Label: %d' % (np.argmax(predictions[index]), | |
| np.argmax(y_test[index]))) | |
| plt.imshow(x_test[index].reshape(28,28), cmap='gray') |
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
| loss = tf.losses.softmax_cross_entropy(onehot_labels=labels, logits=y) | |
| step = tf.train.AdamOptimizer().minimize(loss) |
NewerOlder