Skip to content

Commit

Permalink
add como
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipVinc committed Apr 3, 2022
1 parent 2cb2e07 commit 110056d
Show file tree
Hide file tree
Showing 13 changed files with 9,445 additions and 0 deletions.
2,494 changes: 2,494 additions & 0 deletions 21como-intro-to-deeplearning/1-start.ipynb

Large diffs are not rendered by default.

821 changes: 821 additions & 0 deletions 21como-intro-to-deeplearning/1c-autoreg.ipynb

Large diffs are not rendered by default.

1,061 changes: 1,061 additions & 0 deletions 21como-intro-to-deeplearning/2a-mnist.ipynb

Large diffs are not rendered by default.

902 changes: 902 additions & 0 deletions 21como-intro-to-deeplearning/2b-generative.ipynb

Large diffs are not rendered by default.

972 changes: 972 additions & 0 deletions 21como-intro-to-deeplearning/3a-policy-gradient.ipynb

Large diffs are not rendered by default.

811 changes: 811 additions & 0 deletions 21como-intro-to-deeplearning/3b-policy-gradient-harder.ipynb

Large diffs are not rendered by default.

621 changes: 621 additions & 0 deletions 21como-intro-to-deeplearning/3c-cartpole.ipynb

Large diffs are not rendered by default.

1,663 changes: 1,663 additions & 0 deletions 21como-intro-to-deeplearning/4-nqs-ising.ipynb

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions 21como-intro-to-deeplearning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Lecture notes : Introduction to Deep Learning

## Giuseppe Carleo and Filippo Vicentini

Doctoral course at Università degli studi dell'Insubria

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 21como-intro-to-deeplearning/images/mnist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 94 additions & 0 deletions 21como-intro-to-deeplearning/vae_utils_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Copyright 2021 The Flax Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This code is created with reference to torchvision/utils.py.
Modify: torch.tensor -> jax.numpy.DeviceArray
If you want to know about this file in detail, please visit the original code:
https://github.com/pytorch/vision/blob/master/torchvision/utils.py
"""
import math
from PIL import Image

import jax
import jax.numpy as jnp


def save_image(ndarray, fp, nrow=8, padding=2, pad_value=0.0, format=None):
"""Make a grid of images and Save it into an image file.
Args:
ndarray (array_like): 4D mini-batch images of shape (B x H x W x C)
fp - A filename(string) or file object
nrow (int, optional): Number of images displayed in each row of the grid.
The final grid size is ``(B / nrow, nrow)``. Default: ``8``.
padding (int, optional): amount of padding. Default: ``2``.
scale_each (bool, optional): If ``True``, scale each image in the batch of
images separately rather than the (min, max) over all images. Default: ``False``.
pad_value (float, optional): Value for the padded pixels. Default: ``0``.
format(Optional): If omitted, the format to use is determined from the filename extension.
If a file object was used instead of a filename, this parameter should always be used.
"""
if not (isinstance(ndarray, jnp.ndarray) or
(isinstance(ndarray, list) and all(isinstance(t, jnp.ndarray) for t in ndarray))):
raise TypeError('array_like of tensors expected, got {}'.format(type(ndarray)))

ndarray = jnp.asarray(ndarray)

if ndarray.ndim == 4 and ndarray.shape[-1] == 1: # single-channel images
ndarray = jnp.concatenate((ndarray, ndarray, ndarray), -1)

# make the mini-batch of images into a grid
nmaps = ndarray.shape[0]
xmaps = min(nrow, nmaps)
ymaps = int(math.ceil(float(nmaps) / xmaps))
height, width = int(ndarray.shape[1] + padding), int(ndarray.shape[2] + padding)
num_channels = ndarray.shape[3]
grid = jnp.full((height * ymaps + padding, width * xmaps + padding, num_channels), pad_value).astype(jnp.float32)
k = 0
for y in range(ymaps):
for x in range(xmaps):
if k >= nmaps:
break
grid = jax.ops.index_update(
grid, jax.ops.index[y * height + padding:(y + 1) * height,
x * width + padding:(x + 1) * width],
ndarray[k])
k = k + 1

# Add 0.5 after unnormalizing to [0, 255] to round to nearest integer
ndarr = jnp.clip(grid * 255.0 + 0.5, 0, 255).astype(jnp.uint8)
im = Image.fromarray(ndarr.copy())
im.save(fp, format=format)

# Utility functions (don't worry. you don't need to understand this one)
from matplotlib import pyplot as plt

def show_img(img, ax=None, title=None):
"""Shows a single image."""
if ax is None:
ax = plt.gca()
ax.imshow(img[..., 0], cmap='gray')
ax.set_xticks([])
ax.set_yticks([])
if title:
ax.set_title(title)

def show_img_grid(imgs, titles):
"""Shows a grid of images."""
n = int(np.ceil(len(imgs)**.5))
_, axs = plt.subplots(n, n, figsize=(3 * n, 3 * n))
for i, (img, title) in enumerate(zip(imgs, titles)):
show_img(img, axs[i // n][i % n], title)

0 comments on commit 110056d

Please sign in to comment.