Jupyter notebooks for the code samples of the book "Deep Learning with Python"

Overview

Companion Jupyter notebooks for the book "Deep Learning with Python"

This repository contains Jupyter notebooks implementing the code samples found in the book Deep Learning with Python, 2nd Edition (Manning Publications).

For readability, these notebooks only contain runnable code blocks and section titles, and omit everything else in the book: text paragraphs, figures, and pseudocode. If you want to be able to follow what's going on, I recommend reading the notebooks side by side with your copy of the book.

These notebooks use Python 3.7 and Keras 2.0.8. They were generated on a p2.xlarge EC2 instance.

Table of contents

Comments
  • Edited codeblock 7.26: Made the CustomModel independent of previous model.

    Edited codeblock 7.26: Made the CustomModel independent of previous model.

    In code block 7.26, If the sub-block alone is executed, it generates NameError: name 'model' is not defined. The subclass CustomModel uses the optimizer and trainable weights that were defined in the previous model. By accessing the actual optimizer and trainable weights that passed to CustomModel's compile(), the subclass can be made independent of the previously defined model and can eliminate the mentioned error.

    opened by var-nan 3
  • 6.3:  AttributeError: module 'numpy.random' has no attribute 'randit'

    6.3: AttributeError: module 'numpy.random' has no attribute 'randit'

    Hello,

    I am currently getting the error: AttributeError: module 'numpy.random' has no attribute 'randit' when running model.fit_generator(). However, the numpy.random.randit() works fine when we call generator() directly.

    I have run pip3 install numpy as well as python3 -m pip install numpy to make sure numpy is update.

    I am currently running a jupyter notebook on a Ubuntu 16.04 machine.

    opened by combstraight 3
  • Unclear how to download data used in 6.3

    Unclear how to download data used in 6.3

    The instructions in 6.3 say the data is

    recorded at the Weather Station at the Max-Planck-Institute for Biogeochemistry in Jena, Germany: http://www.bgc-jena.mpg.de/wetter/.

    The data file name in the code is jena_climate_2009_2016.csv. The website allows us to download data in 6-month increments, so the period 2009-2016 is split into 16 files.

    Were these files concatenated to form the file used in the notebook? If so, a sentence to that effect might clarify where the data came from. Alternatively, if jena_climate_2009_2016.csv isn't the concatenation of these files, I think it's unclear where a reader would find the data.

    opened by dansbecker 3
  • issue with Colab (second edition) chapter 11 part 1

    issue with Colab (second edition) chapter 11 part 1

    Hi @fchollet, thanks for this amazing book (and the corresponding colab notebooks).

    I tried to run this file https://colab.research.google.com/github/fchollet/deep-learning-with-python-notebooks/blob/master/chapter11_part01_introduction.ipynb#scrollTo=xdw1FYamgsP9

    But I am getting the following error

    from tensorflow.keras.layers import TextVectorization
    text_vectorization = TextVectorization(
        output_mode="int",
    )
    ---------------------------------------------------------------------------
    ImportError                               Traceback (most recent call last)
    <ipython-input-1-0642862b90e9> in <module>()
    ----> 1 from tensorflow.keras.layers import TextVectorization
          2 text_vectorization = TextVectorization(
          3     output_mode="int",
          4 )
    
    ImportError: cannot import name 'TextVectorization' from 'tensorflow.keras.layers' (/usr/local/lib/python3.7/dist-packages/tensorflow/keras/layers/__init__.py)
    

    Do you know what the issue is? Thanks!

    opened by randomgambit 2
  • optimizers.RMSprop

    optimizers.RMSprop

    I am trying some codes from your fantastic book, but got unconventional error . Please give an eye on it . Thank you model.compile(optimizer=optimizers.RMSprop(learning_rate=0.001), loss='binary_crossentropy' , metrics=['accuracy']) (on p-73) Error : module 'keras.optimizers' has no attribute 'RMSprop'

    opened by Raghav-Bell 2
  • 6.3 jena_climate target leakage problem

    6.3 jena_climate target leakage problem

    The author foget deleting the temperature column in the float_data which is used as a pool for sampling. Thus the outcome looks perfect due to the target leakage.

    Delete the target response when try the code yourself, though it will let u see how bad the Network performs. 😄

    temp = float_data[:, 1] a = float_data[:, 0] a = np.reshape(a, newshape=(len(a), 1)) b = float_data[:, 2:] print(a.shape, b.shape) float_data = np.concatenate([a, b], axis=1)

    opened by Dolores2333 2
  • 5.4-visualizing-what-convnets-learn input_13:0 is both fed and fetched error

    5.4-visualizing-what-convnets-learn input_13:0 is both fed and fetched error

    Using Keras 2.2.4, I'm working my way though this notebook 5.4-visualizing-what-convnets-learn , except I switched the model with a unet one provided by Kaggle-Carvana-Image-Masking-Challenge . The first layer of the Kaggle model looks like this, followed by the rest of the example code.

    def get_unet_512(input_shape=(512, 512, 3),
                     num_classes=1):
        inputs = Input(shape=input_shape)
    
    ...
    
    Layer (type)                    Output Shape         Param #     Connected to                     
    ==================================================================================================
    input_13 (InputLayer)           (None, 512, 512, 3)  0    
    ...
    
    from keras import models
    layer_outputs = [layer.output for layer in model.layers[:8]]
    activation_model = models.Model(inputs=model.input, outputs=layer_outputs)
    activations = activation_model.predict(img_tensor)
    
    

    Now the error I am getting is

    InvalidArgumentError: input_13:0 is both fed and fetched.
    

    Does anyone have any suggestions on how to work around this?

    opened by matthewchung74 2
  • Listing 6.37. Training and evaluating a densely connected model - stalls on first epoch

    Listing 6.37. Training and evaluating a densely connected model - stalls on first epoch

    Having trouble getting listing 6.37 to work. The model stalls on the first epoch. Getting the following output, but it never reaches the end of epoch 1:

    Epoch 1/20
    496/500 [============================>.] - ETA: 0s - loss: 1.2985
    

    Prior code in this chapter and code in prior chapters works fine. Any suggestions?

    Thanks,

    opened by jswift24 2
  • Chap 07: AttributeError: 'SparseCategoricalAccuracy' object has no attribute 'reset_state'

    Chap 07: AttributeError: 'SparseCategoricalAccuracy' object has no attribute 'reset_state'

    • title above code block: Writing a step-by-step training loop: the loop itself

    • error output: Screenshot from 2021-12-31 09-05-29

    • TF, TF-base v2.4.1; TF-estimator v2.6.0

    • conda v4.11.0

    • Ubuntu 21.10

    opened by bjpcjp 1
  • listing 3.10 should history key be

    listing 3.10 should history key be "accuracy" and "val_accuracy" instead of "acc" and "val_acc"?

    On the book:

    acc = history.history['acc']
    val_acc = history.history['val_acc']
    

    But if I typed so, it would result in an error where keys of history are incorrect. But when I did history.history.keys(), it showed "accuracy" and "val_accuracy" instead. Is this a mistake on my part?

    Here's the full code:

    from keras.datasets import reuters
    from tensorflow.keras import models
    from tensorflow.keras import layers
    import numpy as np
    import matplotlib.pyplot as plt
    
    def vectorize_sequences(sequences, dimension = 10000):
    	results = np.zeros((len(sequences),dimension))
    	for i, sequence in enumerate(sequences):
    		results[i, sequence] = 1
    	return results
    
    def to_one_hot(labels, dimension=46):
    	results = np.zeros((len(labels), dimension))
    	for i, label in enumerate(labels):
    		results[i, label] = 1
    	return results
    
    (train_data, train_labels), (test_data, test_labels) = reuters.load_data(num_words=10000)
    
    x_train = vectorize_sequences(train_data)
    x_test = vectorize_sequences(test_data)
    
    one_hot_train_label = to_one_hot(train_labels)
    one_hot_test_label = to_one_hot(test_labels)
    
    # one_hot_train_label = to_categorical(train_labels)
    # one_hot_test_label = to_categorical(test_labels)
    
    model = models.Sequential()
    model.add(layers.Dense(64, activation='relu', input_shape=(10000,)))
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dense(46, activation='softmax'))
    
    model.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    
    x_val = x_train[:1000]
    partial_x_train = x_train[1000:]
    
    y_val = one_hot_train_label[:1000]
    partial_y_train = one_hot_train_label[1000:]
    
    history = model.fit(partial_x_train,
                        partial_y_train,
                        epochs=20,
                        batch_size=512,
                        validation_data=(x_val, y_val))
    
    print("History key: ", history.history.keys())
    
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    
    epochs = range(1, len(loss) + 1)
    
    plt.plot(epochs, loss, 'bo', label='Training loss')
    plt.plot(epochs, val_loss, 'b', label='Validation loss')
    plt.title('Training and validation loss')
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend()
    
    plt.show()
    
    plt.clf()   # clear figure
    
    acc = history.history['accuracy']
    val_acc = history.history['val_accuracy']
    
    plt.plot(epochs, acc, 'bo', label='Training acc')
    plt.plot(epochs, val_acc, 'b', label='Validation acc')
    plt.title('Training and validation accuracy')
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend()
    
    plt.show()
    
    opened by warrenbocphet 1
  • VGG 16 layers not loading

    VGG 16 layers not loading

    Hi,

    I tried to use vgg16 using keras for the first time. I loaded model offline in weights parameter using file path. on model.summary it is not showing flatten, fc1, fc2 layers . it shows layers up to bock5_pool . why so ? on proceeding further, I am getting model output predictions of the type (1,512,7,7) which shows error in decode_predictions ..Pls help

    opened by jaigsingla 1
  • Chapter 11, part 2, cannot load Glove

    Chapter 11, part 2, cannot load Glove

    When trying to run the code for preparing Glove word-embeddings matrix, towards the end of the notebook for chapter 11, part 2, I get an error: embedding_dim = 100

    Retrieve the vocabulary indexed by our previous TextVectorization layer.

    vocabulary = text_vectorization.get_vocabulary()

    Use it to create a mapping from words to their index in the vocabulary.

    word_index = dict(zip(vocabulary, range(len(vocabulary))))

    Prepare a matrix that will be filled with the GloVe vectors.

    embedding_matrix = np.zeros((max_tokens, embedding_dim)) for word, i in word_index.items(): if i < max_tokens: embedding_vector = embeddings_index.get(word) # Fill entry i in the matrix with the word vector for index i. # Words not found in the embedding index will be all zeros. if embedding_vector is not None: embedding_matrix[i] = embedding_vector UnicodeDecodeError Traceback (most recent call last) Input In [29], in <cell line: 4>() 1 embedding_dim = 100 3 # Retrieve the vocabulary indexed by our previous TextVectorization layer. ----> 4 vocabulary = text_vectorization.get_vocabulary() 5 # Use it to create a mapping from words to their index in the vocabulary. 6 word_index = dict(zip(vocabulary, range(len(vocabulary))))

    File C:\ProgramData\Anaconda3\envs\tf-gpu\lib\site-packages\keras\layers\preprocessing\text_vectorization.py:448, in TextVectorization.get_vocabulary(self, include_special_tokens) 439 def get_vocabulary(self, include_special_tokens=True): 440 """Returns the current vocabulary of the layer. 441 442 Args: (...) 446 vocabulary will not include any padding or OOV tokens. 447 """ --> 448 return self._lookup_layer.get_vocabulary(include_special_tokens)

    File C:\ProgramData\Anaconda3\envs\tf-gpu\lib\site-packages\keras\layers\preprocessing\index_lookup.py:336, in IndexLookup.get_vocabulary(self, include_special_tokens) 334 keys, values = self.lookup_table.export() 335 vocab, indices = (values, keys) if self.invert else (keys, values) --> 336 vocab, indices = (self._tensor_vocab_to_numpy(vocab), indices.numpy()) 337 lookup = collections.defaultdict(lambda: self.oov_token, 338 zip(indices, vocab)) 339 vocab = [lookup[x] for x in range(self.vocabulary_size())]

    File C:\ProgramData\Anaconda3\envs\tf-gpu\lib\site-packages\keras\layers\preprocessing\string_lookup.py:401, in StringLookup._tensor_vocab_to_numpy(self, vocabulary) 399 def _tensor_vocab_to_numpy(self, vocabulary): 400 vocabulary = vocabulary.numpy() --> 401 return np.array([tf.compat.as_text(x, self.encoding) for x in vocabulary])

    File C:\ProgramData\Anaconda3\envs\tf-gpu\lib\site-packages\keras\layers\preprocessing\string_lookup.py:401, in (.0) 399 def _tensor_vocab_to_numpy(self, vocabulary): 400 vocabulary = vocabulary.numpy() --> 401 return np.array([tf.compat.as_text(x, self.encoding) for x in vocabulary])

    File C:\ProgramData\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\util\compat.py:110, in as_text(bytes_or_text, encoding) 108 return bytes_or_text 109 elif isinstance(bytes_or_text, bytes): --> 110 return bytes_or_text.decode(encoding) 111 else: 112 raise TypeError('Expected binary or unicode string, got %r' % bytes_or_text)

    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: unexpected end of data

    opened by zdjordje123 1
  • gdown --id warning!

    gdown --id warning!

    FutureWarning: Option --id was deprecated in version 4.3.1 and will be removed in 5.0. You don't need to pass it anymore to use a file ID.

    https://github.com/fchollet/deep-learning-with-python-notebooks/blob/master/chapter12_part05_gans.ipynb****

    opened by ehzawad 0
  • 2nd edition Chapter 12, VAE latent space sampler differences with 1st edition

    2nd edition Chapter 12, VAE latent space sampler differences with 1st edition

    In the 2nd edition notebook of chapter 12 part 4, the latent space sampling layer is defined as the following class:

    class Sampler(layers.Layer):
        def call(self, z_mean, z_log_var):
            batch_size = tf.shape(z_mean)[0]
            z_size = tf.shape(z_mean)[1]
            epsilon = tf.random.normal(shape=(batch_size, z_size))
            return z_mean + tf.exp(0.5 * z_log_var) * epsilon
    

    In the 1st edition, it's defined as the following fucntion:

    def sampling(args):
        z_mean, z_log_var = args
        epsilon = K.random_normal(shape=(K.shape(z_mean)[0], 2),
                                              mean=0., stddev=1.)
        return z_mean + K.exp(z_log_var) * epsilon
    

    Most changes come from following best practices and API changes, but I do have the folowing doubt: Why is z_log_var multiplied by 0.5 in the 2nd edition class? Is it because a difference between tf.exp() and tf.keras.backend.exp()? Is it becasue reducing it by half yields better results? Any other reason?

    opened by ghylander 0
  • question: why is multi-hot encoding appropriate for sequences?

    question: why is multi-hot encoding appropriate for sequences?

    In Deep Learning with Python 2e, @fchollet says:

    Multi-hot encode your lists to turn them into vectors of 0s and 1s. This would mean, for instance, turning the sequence [8, 5] into a 10,000-dimensional vector that would be all 0s except for indices 8 and 5, which would be 1s.

    Wouldn’t this encoding essentially reduce the sequence to a set, which means losing information? For instance, how would the encoded representation of [8, 5] differ from [5, 8] or [8, 5, 8] (or any longer sequence of 8 and 5 elements)?

    (This example arises in a tutorial about classifying movie reviews by whether they’re positive or negative. To make the example concrete, if word 5 is pretty and word 8 is awful, then there’s going to be a classification difference between pretty awful and awful pretty!)

    opened by mbutterick 0
  • 6.1.3 Why did I get worse when I increased the sample size?

    6.1.3 Why did I get worse when I increased the sample size?

    Following the example in the book, I increased the sample size from 200 to 3000 and added a dropout regularization, but the final validation accuracy is only 50%. 1661074009976

    opened by fangxuehouwuming 4
  • Mask for `TransformerDecoder` in the end-to-end Transformer (chapter11_part04_sequence-to-sequence-learning.ipynb)

    Mask for `TransformerDecoder` in the end-to-end Transformer (chapter11_part04_sequence-to-sequence-learning.ipynb)

    In the chapter11_part04_sequence-to-sequence-learning.ipynb, the TransformerDecoder receives the mask from the PositionalEmbedding layer of the target sequence:

    x = PositionalEmbedding(sequence_length, vocab_size, embed_dim)(decoder_inputs)
    x = TransformerDecoder(embed_dim, dense_dim, num_heads)(x, encoder_outputs)
    

    Shouldn’t the mask be the one created from encoding the source sequence?

    For example, I have seen that in this TF tutorial the mask from the source sequence is used instead.

    Any clarification would be greatly appreciated.

    opened by balvisio 0
Owner
François Chollet
François Chollet
Repository for scripts and notebooks from the book: Programming PyTorch for Deep Learning

Repository for scripts and notebooks from the book: Programming PyTorch for Deep Learning

Ian Pointer 368 Dec 17, 2022
📚 A collection of Jupyter notebooks for learning and experimenting with OpenVINO 👓

A collection of ready-to-run Python* notebooks for learning and experimenting with OpenVINO developer tools. The notebooks are meant to provide an introduction to OpenVINO basics and teach developers how to leverage our APIs for optimized deep learning inference in their applications.

OpenVINO Toolkit 840 Jan 3, 2023
Library extending Jupyter notebooks to integrate with Apache TinkerPop and RDF SPARQL.

Graph Notebook: easily query and visualize graphs The graph notebook provides an easy way to interact with graph databases using Jupyter notebooks. Us

Amazon Web Services 501 Dec 28, 2022
Sample code from the Neural Networks from Scratch book.

Neural Networks from Scratch (NNFS) book code Code from the NNFS book (https://nnfs.io) separated by chapter.

Harrison 172 Dec 31, 2022
Like Dirt-Samples, but cleaned up

Clean-Samples Like Dirt-Samples, but cleaned up, with clear provenance and license info (generally a permissive creative commons licence but check the

TidalCycles 39 Nov 30, 2022
PAWS 🐾 Predicting View-Assignments with Support Samples

This repo provides a PyTorch implementation of PAWS (predicting view assignments with support samples), as described in the paper Semi-Supervised Learning of Visual Features by Non-Parametrically Predicting View Assignments with Support Samples.

Facebook Research 437 Dec 23, 2022
Learn about Spice.ai with in-depth samples

Samples Learn about Spice.ai with in-depth samples ServerOps - Learn when to run server maintainance during periods of low load Gardener - Intelligent

Spice.ai 16 Mar 23, 2022
Training Confidence-Calibrated Classifier for Detecting Out-of-Distribution Samples / ICLR 2018

Training Confidence-Calibrated Classifier for Detecting Out-of-Distribution Samples This project is for the paper "Training Confidence-Calibrated Clas

null 168 Nov 29, 2022
Repo for FUZE project. I will also publish some Linux kernel LPE exploits for various real world kernel vulnerabilities here. the samples are uploaded for education purposes for red and blue teams.

Linux_kernel_exploits Some Linux kernel exploits for various real world kernel vulnerabilities here. More exploits are yet to come. This repo contains

Wei Wu 472 Dec 21, 2022
NeurIPS 2021, "Fine Samples for Learning with Noisy Labels"

[Official] FINE Samples for Learning with Noisy Labels This repository is the official implementation of "FINE Samples for Learning with Noisy Labels"

mythbuster 27 Dec 23, 2022
Qimera: Data-free Quantization with Synthetic Boundary Supporting Samples

Qimera: Data-free Quantization with Synthetic Boundary Supporting Samples This repository is the official implementation of paper [Qimera: Data-free Q

Kanghyun Choi 21 Nov 3, 2022
The Malware Open-source Threat Intelligence Family dataset contains 3,095 disarmed PE malware samples from 454 families

MOTIF Dataset The Malware Open-source Threat Intelligence Family (MOTIF) dataset contains 3,095 disarmed PE malware samples from 454 families, labeled

Booz Allen Hamilton 112 Dec 13, 2022
Final project for machine learning (CSC 590). Detection of hepatitis C and progression through blood samples.

Hepatitis C Blood Based Detection Final project for machine learning (CSC 590). Dataset from Kaggle. Using data from previous hepatitis C blood panels

Jennefer Maldonado 1 Dec 28, 2021
Analysis of Antarctica sequencing samples contaminated with SARS-CoV-2

Analysis of SARS-CoV-2 reads in sequencing of 2018-2019 Antarctica samples in PRJNA692319 The samples analyzed here are described in this preprint, wh

Jesse Bloom 4 Feb 9, 2022
Ready-to-use code and tutorial notebooks to boost your way into few-shot image classification.

Easy Few-Shot Learning Ready-to-use code and tutorial notebooks to boost your way into few-shot image classification. This repository is made for you

Sicara 399 Jan 8, 2023
Source code and notebooks to reproduce experiments and benchmarks on Bias Faces in the Wild (BFW).

Face Recognition: Too Bias, or Not Too Bias? Robinson, Joseph P., Gennady Livitz, Yann Henon, Can Qin, Yun Fu, and Samson Timoner. "Face recognition:

Joseph P. Robinson 41 Dec 12, 2022
Experimental solutions to selected exercises from the book [Advances in Financial Machine Learning by Marcos Lopez De Prado]

Advances in Financial Machine Learning Exercises Experimental solutions to selected exercises from the book Advances in Financial Machine Learning by

Brian 1.4k Jan 4, 2023
Official repository of my book: "Deep Learning with PyTorch Step-by-Step: A Beginner's Guide"

This is the official repository of my book "Deep Learning with PyTorch Step-by-Step". Here you will find one Jupyter notebook for every chapter in the book.

Daniel Voigt Godoy 340 Jan 1, 2023
Free Book about Deep-Learning approaches for Chess (like AlphaZero, Leela Chess Zero and Stockfish NNUE)

Free Book about Deep-Learning approaches for Chess (like AlphaZero, Leela Chess Zero and Stockfish NNUE)

Dominik Klein 189 Dec 21, 2022