程序代写代做代考 python Untitled-checkpoint

Untitled-checkpoint

In [1]:

from scipy.ndimage.interpolation import rotate
from mlp.data_providers import AugmentedMNISTDataProvider, MNISTDataProvider
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import logging
from mlp.layers import AffineLayer, SoftmaxLayer, SigmoidLayer, ReluLayer
from mlp.errors import CrossEntropyError, CrossEntropySoftmaxError
from mlp.models import SingleLayerModel, MultipleLayerModel
from mlp.initialisers import UniformInit
from mlp.learning_rules import GradientDescentLearningRule
from mlp.data_providers import MNISTDataProvider
from mlp.optimisers import Optimiser

import os
os.environ[‘MLP_DATA_DIR’] = ‘/Users/vagrant/tasks-2016/python-deep-learning-4200/mlpractical-mlp2016-7-coursework2/data’

plt.style.use(‘ggplot’)

import numpy as np
from collections import OrderedDict
import logging
from mlp.layers import AffineLayer
from mlp.errors import CrossEntropySoftmaxError
from mlp.models import MultipleLayerModel
from mlp.initialisers import GlorotUniformInit, ConstantInit
from mlp.learning_rules import MomentumLearningRule
from mlp.data_providers import MNISTDataProvider
from mlp.optimisers import Optimiser

# Seed a random number generator
seed = 24102016
rng = np.random.RandomState(seed)

num_epochs = 100
stats_interval = 5
batch_size = 50
learning_rate = 0.01
mom_coeff = 0.9
weights_init_gain = 0.5
biases_init = 0.

# Set up a logger object to print info about the training run to stdout
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.handlers = [logging.StreamHandler()]

# Create data provider objects for the MNIST data set
train_data = MNISTDataProvider(‘train’, batch_size, rng=rng)
valid_data = MNISTDataProvider(‘valid’, batch_size, rng=rng)
input_dim, output_dim, hidden_dim = 784, 10, 100

weights_init = GlorotUniformInit(weights_init_gain, rng)
biases_init = ConstantInit(biases_init)
error = CrossEntropySoftmaxError()
learning_rule = MomentumLearningRule(learning_rate, mom_coeff)
data_monitors = {‘acc’: lambda y, t: (y.argmax(-1) == t.argmax(-1)).mean()}

def random_rotation(inputs, rng):
“””Randomly rotates a subset of images in a batch.

Args:
inputs: Input image batch, an array of shape (batch_size, 784).
rng: A seeded random number generator.

Returns:
An array of shape (batch_size, 784) corresponding to a copy
of the original `inputs` array with the randomly selected
images rotated by a random angle. The original `inputs`
array should not be modified.
“””
orig_ims = inputs.reshape((-1, 28, 28))
new_ims = orig_ims.copy()
indices = rng.choice(orig_ims.shape[0], orig_ims.shape[0] // 4, False)
angles = rng.uniform(-1., 1., size=indices.shape[0]) * 30.
for i, j in enumerate(indices):
new_ims[j] = rotate(orig_ims[j], angles[i], order=1, reshape=False)
return new_ims.reshape((-1, 784))

In [2]:

def show_batch_of_images(img_batch, fig_size=(3, 3)):
fig = plt.figure(figsize=fig_size)
batch_size, im_height, im_width = img_batch.shape
# calculate no. columns per grid row to give square grid
grid_size = int(batch_size**0.5)
# intialise empty array to tile image grid into
tiled = np.empty((im_height * grid_size,
im_width * batch_size // grid_size))
# iterate over images in batch + indexes within batch
for i, img in enumerate(img_batch):
# calculate grid row and column indices
r, c = i % grid_size, i // grid_size
tiled[r * im_height:(r + 1) * im_height,
c * im_height:(c + 1) * im_height] = img
ax = fig.add_subplot(111)
ax.imshow(tiled, cmap=’Greys’) #, vmin=0., vmax=1.)
ax.axis(‘off’)
fig.tight_layout()
plt.show()
return fig, ax

# Seed a random number generator
seed = 6102016
rng = np.random.RandomState(seed)

test_data = MNISTDataProvider(‘test’, 100, rng=rng)
inputs, targets = test_data.next()
_ = show_batch_of_images(inputs.reshape((-1, 28, 28)))
transformed_inputs = random_rotation(inputs, rng)
_ = show_batch_of_images(transformed_inputs.reshape((-1, 28, 28)))

In [28]:

import random
from scipy.ndimage.interpolation import rotate, shift, zoom
def random_shift(inputs, rng):
“””Randomly rotates a subset of images in a batch.

Args:
inputs: Input image batch, an array of shape (batch_size, 784).
rng: A seeded random number generator.

Returns:
An array of shape (batch_size, 784) corresponding to a copy
of the original `inputs` array with the randomly selected
images rotated by a random angle. The original `inputs`
array should not be modified.
“””
orig_ims = inputs.reshape((-1, 28, 28))
new_ims = orig_ims.copy()
# indices = rng.choice(orig_ims.shape[0], orig_ims.shape[0] // 4, False)
# angles = rng.uniform(-1., 1., size=indices.shape[0]) * 30.
# for i, j in enumerate(indices):
# new_ims[j] = rotate(orig_ims[j], angles[i], order=1, reshape=False)

for j in range(orig_ims.shape[0]):
# new_ims[j] = shift(orig_ims[j], random.randint(-4, 4), order = 1)

new_ims[j] = zoom(orig_ims[j], 1.1, order = 1)

return new_ims.reshape((-1, 784))

In [27]:

seed = 6102016
rng = np.random.RandomState(seed)

test_data = MNISTDataProvider(‘test’, 100, rng=rng)
inputs, targets = test_data.next()
_ = show_batch_of_images(inputs.reshape((-1, 28, 28)))
transformed_inputs = random_shift(inputs, rng)
_ = show_batch_of_images(transformed_inputs.reshape((-1, 28, 28)))

—————————————————————————
ValueError Traceback (most recent call last)
in ()
5 inputs, targets = test_data.next()
6 _ = show_batch_of_images(inputs.reshape((-1, 28, 28)))
—-> 7 transformed_inputs = random_shift(inputs, rng)
8 _ = show_batch_of_images(transformed_inputs.reshape((-1, 28, 28)))

in random_shift(inputs, rng)
24 # new_ims[j] = shift(orig_ims[j], random.randint(-4, 4), order = 1)
25
—> 26 new_ims[j] = zoom(orig_ims[j], 1.2, order = 1)
27
28 return new_ims.reshape((-1, 784))

ValueError: could not broadcast input array from shape (34,34) into shape (28,28)

In [18]:

show_batch_of_images(inputs.reshape((-1, 28, 28)))

Out[18]:

(,
)

In [29]:

rng.choice(100, 3)

Out[29]:

array([7, 3, 2])

In [40]:

def random_rotate_shift(inputs, rng):
“””Randomly rotates a subset of images in a batch.

Args:
inputs: Input image batch, an array of shape (batch_size, 784).
rng: A seeded random number generator.

Returns:
An array of shape (batch_size, 784) corresponding to a copy
of the original `inputs` array with the randomly selected
images rotated by a random angle. The original `inputs`
array should not be modified.
“””
orig_ims = inputs.reshape((-1, 28, 28))
new_ims = orig_ims.copy()
indices = rng.choice(orig_ims.shape[0], orig_ims.shape[0] // 2, False)
angles = rng.uniform(-1., 1., size=indices.shape[0]) * 30.
for i, j in enumerate(indices):
new_ims[j] = rotate(orig_ims[j], angles[i], order=1, reshape=False)

indices = rng.choice(orig_ims.shape[0], orig_ims.shape[0] // 2, False)
shifts = rng.uniform(-1., 1., size=indices.shape[0]) * 4.

for i, j in enumerate(indices):
new_ims[j] = shift(new_ims[j], shifts[i], order=1)

return new_ims.reshape((-1, 784))

In [41]:

seed = 6102016
rng = np.random.RandomState(seed)

test_data = MNISTDataProvider(‘test’, 100, rng=rng)
inputs, targets = test_data.next()
_ = show_batch_of_images(inputs.reshape((-1, 28, 28)))
transformed_inputs = random_rotate_shift(inputs, rng)
_ = show_batch_of_images(transformed_inputs.reshape((-1, 28, 28)))

_ = show_batch_of_images(inputs.reshape((-1, 28, 28)))

In [ ]:

In [ ]:

Posted in Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *