Skip to content

Instantly share code, notes, and snippets.

View numpy_and_tf_at_the_same_time.py
from keras import backend
from keras.backend import numpy_backend
import numpy as np
import tensorflow as tf
class NPTF(object):
def __getattr__(self, name):
if name in dir(numpy_backend) and name in dir(backend):
@fchollet
fchollet / imperative_symbolic_blend.py
Created Oct 5, 2018
Blending Imperative and Symbolic differentiable programming
View imperative_symbolic_blend.py
############################################################################
# Case 1: inserting non-layer ops into a graph of layers
############################################################################
input_1 = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4)(input_1)
output = tf.exp(x) # !!
model = tf.keras.Model(input_1, output)
############################################################################
@fchollet
fchollet / new_stacked_rnns.py
Last active Aug 13, 2019
New stacked RNNs in Keras
View new_stacked_rnns.py
import keras
import numpy as np
timesteps = 60
input_dim = 64
samples = 10000
batch_size = 128
output_dim = 64
# Test data.
View small_xception.py
"""Downsized version of Xception, without residual connections.
"""
from __future__ import print_function
from __future__ import absolute_import
from keras.models import Model
from keras.layers import Dense
from keras.layers import Input
from keras.layers import BatchNormalization
from keras.layers import Activation
View keras_logistic_regression.py
from keras.models import Sequential
from keras.layers import Dense
x, y = ...
x_val, y_val = ...
# 1-dimensional MSE linear regression in Keras
model = Sequential()
model.add(Dense(1, input_dim=x.shape[1]))
model.compile(optimizer='rmsprop', loss='mse')
@fchollet
fchollet / classifier_from_little_data_script_3.py
Last active Oct 27, 2020
Fine-tuning a Keras model. Updated to the Keras 2.0 API.
View classifier_from_little_data_script_3.py
'''This script goes along the blog post
"Building powerful image classification models using very little data"
from blog.keras.io.
It uses data that can be downloaded at:
https://www.kaggle.com/c/dogs-vs-cats/data
In our setup, we:
- created a data/ folder
- created train/ and validation/ subfolders inside data/
- created cats/ and dogs/ subfolders inside train/ and validation/
- put the cat pictures index 0-999 in data/train/cats
View classifier_from_little_data_script_2.py
'''This script goes along the blog post
"Building powerful image classification models using very little data"
from blog.keras.io.
It uses data that can be downloaded at:
https://www.kaggle.com/c/dogs-vs-cats/data
In our setup, we:
- created a data/ folder
- created train/ and validation/ subfolders inside data/
- created cats/ and dogs/ subfolders inside train/ and validation/
- put the cat pictures index 0-999 in data/train/cats
View classifier_from_little_data_script_1.py
'''This script goes along the blog post
"Building powerful image classification models using very little data"
from blog.keras.io.
It uses data that can be downloaded at:
https://www.kaggle.com/c/dogs-vs-cats/data
In our setup, we:
- created a data/ folder
- created train/ and validation/ subfolders inside data/
- created cats/ and dogs/ subfolders inside train/ and validation/
- put the cat pictures index 0-999 in data/train/cats
View functional_keras.py
'''Functional Keras is a more functional replacement for the Graph API.
'''
###################
# 2 LSTM branches #
###################
a = Input(input_shape=(10, 32)) # output is a TF/TH placeholder, augmented with Keras attributes
b = Input(input_shape=(10, 32))
encoded_a = LSTM(32)(a) # output is a TF/TH tensor
encoded_b = LSTM(32)(b)
@fchollet
fchollet / keras_intermediate.py
Created May 28, 2015
Defining a Theano function to output intermediate transformations in a Keras model
View keras_intermediate.py
import theano
from keras.models import Sequential
from keras.layers.core import Dense, Activation
X_train, y_train = ... # load some training data
X_batch = ... # a batch of test data
# this is your initial model
model = Sequential()
model.add(Dense(20, 64))
You can’t perform that action at this time.