Skip to content

Instantly share code, notes, and snippets.

@TheBojda
TheBojda / minst_gan.py
Last active January 25, 2020 20:08
MINST GAN Example
# MINST GAN Example
# based on https://www.tensorflow.org/tutorials/generative/dcgan
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras import layers, models
import time
BUFFER_SIZE = 60000
BATCH_SIZE = 256
@TheBojda
TheBojda / simplernn.py
Last active January 12, 2020 08:05
Tensorflow SimpleRNN example
import numpy as np
import tensorflow as tf
from tensorflow_core.python.keras import layers, models
text = "Hello World!"
input_text = text[:-1]
target_text = text[1:]
print(input_text)
print(target_text)
@TheBojda
TheBojda / cartpole.py
Created December 9, 2019 08:09
Deep Q-learning CartPole example with OpenAI Gym and Tensorflow
# Deep Q-learning CartPole example
# based on https://github.com/gsurma/cartpole.git & https://gym.openai.com/evaluations/eval_OeUSZwUcR2qSAqMmOE1UIw/
import gym
import random
import numpy as np
from os import path
from collections import deque
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import gym
env = gym.make("Breakout-v0")
observation = env.reset()
for _ in range(10000):
env.render()
action = env.action_space.sample() # your agent here (this takes random actions)
observation, reward, done, info = env.step(action)
if done:
# Word embedding tensorflow example
# based on: https://www.tensorflow.org/tutorials/text/word_embeddings
import io
import tensorflow as tf
from tensorflow_core.python.keras import layers, models, datasets
import tensorflow_datasets as tfds
(train_data, test_data), info = tfds.load(
'imdb_reviews/subwords8k',
@TheBojda
TheBojda / autoencoder.py
Last active November 25, 2019 15:57
Simple autoencoder in Tensorflow, which converts 14 words to 3D vectors
import numpy as np
import tensorflow as tf
from tensorflow_core.python.keras import layers, models
words = ["cat", "dog", "apple", "orange", "car", "airplane", "man", "woman", "drink", "eat", "neural", "network",
"tensor", "flow"]
dict_len = len(words)
word_index = dict((word, i) for i, word in enumerate(words))
@TheBojda
TheBojda / gradtape.py
Created November 10, 2019 10:44
Linear regression with Tensorflow GradientTape
# Linear regression using GradientTape
# based on https://sanjayasubedi.com.np/deeplearning/tensorflow-2-linear-regression-from-scratch/
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
class Model:
def __init__(self):
self.W = tf.Variable(16.0)
# Simple neural network with NumPy from 11 lines of code
# source: https://iamtrask.github.io/2015/07/12/basic-python-network/
X = np.array([ [0,0,1],[0,1,1],[1,0,1],[1,1,1] ])
y = np.array([[0,1,1,0]]).T
syn0 = 2*np.random.random((3,4)) - 1
syn1 = 2*np.random.random((4,1)) - 1
for j in xrange(60000):
l1 = 1/(1+np.exp(-(np.dot(X,syn0))))
l2 = 1/(1+np.exp(-(np.dot(l1,syn1))))
@TheBojda
TheBojda / tensorflow_image_classification.py
Created October 21, 2019 08:31
TensorFlow image classification example
# TensorFlow image classification example
# based on https://www.tensorflow.org/tutorials/keras/classification
# model generation: https://gist.github.com/TheBojda/f297544cc4864b2b10c2aad965339c58
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
from tensorflow.keras.models import load_model
@TheBojda
TheBojda / tensorflow_cnn_model_training.py
Created October 21, 2019 08:19
TensorFlow CNN model training example
# TensorFlow CNN model training example
# based on https://www.tensorflow.org/tutorials/images/cnn
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt