Ejemplo n.º 1
0
# convolutional network on the CIFAR-10 dataset.                                  #
###################################################################################

import time
import numpy as np
import matplotlib.pyplot as plt
import fc_net 
from utils import get_CIFAR10_data
from gradient_check import eval_numerical_gradient, eval_numerical_gradient_array
import solver
from layer_utils import conv_relu_pool_forward, conv_relu_pool_backward
from layer_utils import conv_relu_forward, conv_relu_backward
import layers
import sys

data = get_CIFAR10_data()

###################################################################################
# Overfit small data                                                              #
#                                                                                 #
# A nice trick is to train your model with just a few training                    #
# samples. You should be able to overfit small datasets, which will               #
# result in very high training accuracy and comparatively low validation          #
# accuracy.                                                                       #
###################################################################################

num_train = 100
small_data = {
  'X_train': data['X_train'][:num_train],
  'y_train': data['y_train'][:num_train],
  'X_val': data['X_val'],
import solver
import layer_utils

###################################################################################
#  rel_error function useful for gradient checks                                  #
###################################################################################

def rel_error(x, y):
  """ returns relative error """
  return np.max(np.abs(x - y) / (np.maximum(1e-8, np.abs(x) + np.abs(y))))

###################################################################################
#   Load the (preprocessed) CIFAR10 data.                                         #
###################################################################################

data = utils.get_CIFAR10_data()
for k, v in data.iteritems():
  print '%s: ' % k, v.shape

'''
# Problem 3.1.1
###################################################################################
#   Affine layer: forward.                                                        #
###################################################################################
#   In the file layers.py implement the affine_forward function.                  #
#   Once you are done you can test your implementation by running the following:  #
###################################################################################

# Test the affine_forward function

num_inputs = 2
Ejemplo n.º 3
0
            valtrain = np.mean(predtrain == y_val)
            results[(lr, reg)] = (valtrain, val)
            if val > best_val:
                best_val = val
                best_softmax = softmax

    return best_softmax, results, best_val

################################################################################
#                              END OF YOUR CODE                                #
################################################################################

if __name__ == '__main__':
    # Get the CIFAR-10 data broken up into train, validation and test sets

    X_train, y_train, X_val, y_val, X_test, y_test = utils.get_CIFAR10_data()

    # First implement the naive softmax loss function with nested loops.
    # Open the file softmax.py and implement the
    # softmax_loss_naive function.

    # Generate a random softmax theta matrix and use it to compute the loss.

    theta = np.random.randn(3073,10) * 0.0001
    # loss, grad = softmax_loss_naive(theta, X_train, y_train, 0.0)

    # # Loss should be something close to - log(0.1)

    # print 'loss:', loss, ' should be close to ', - np.log(0.1)

    # Use numeric gradient checking as a debugging tool.
Ejemplo n.º 4
0
import layer_utils

##########################################################################
#  rel_error function useful for gradient checks                                  #
##########################################################################


def rel_error(x, y):
    """ returns relative error """
    return np.max(np.abs(x - y) / (np.maximum(1e-8, np.abs(x) + np.abs(y))))

##########################################################################
#   Load the (preprocessed) CIFAR10 data.                                         #
##########################################################################

data = utils.get_CIFAR10_data()#num_training=29400, num_validation=600, num_test=600)
for k, v in data.iteritems():
    print '%s: ' % k, v.shape


# Problem 3.1.1
##########################################################################
#   Affine layer: forward.                                                        #
##########################################################################
#   In the file layers.py implement the affine_forward function.                  #
#   Once you are done you can test your implementation by running the following:  #
##########################################################################

# Test the affine_forward function

num_inputs = 2
Ejemplo n.º 5
0
# set default plot options
get_ipython().run_line_magic('matplotlib', 'inline')
plt.rcParams['figure.figsize'] = (10.0, 8.0)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

# ## 1. Set up input preprocessing

# In[3]:

from utils import get_CIFAR10_data

# In[4]:

X_tr, Y_tr, X_te, Y_te, mean_img = get_CIFAR10_data()
print('Train data shape : %s,  Train labels shape : %s' %
      (X_tr.shape, Y_tr.shape))
print('Test data shape : %s,  Test labels shape : %s' %
      (X_te.shape, Y_te.shape))

classes = [
    'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
    'ship', 'truck'
]

# ### Input data를 CNN Model의 Input에 맞는 3차원 형태의 데이터로 만들기

# In[10]:

print('X_tr : ', X_tr.shape)
Ejemplo n.º 6
0
    args = parser.parse_args()
    logging.basicConfig(level=logging.INFO)

    # Set distributed flag
    distributed = args.local_rank != -1
    device = torch.device(f"cuda:{args.local_rank}" if (
        torch.cuda.is_available() & distributed) else "cpu")

    if distributed:
        logger.info("Starting distributed training...")
        distr.init_process_group(backend="nccl", init_method="env://")

    ### DATA
    # Get CIFAR10 data
    logger.info("Fetching CIFAR10 data...")
    dset = get_CIFAR10_data(args.data)

    # Set (distributed) dataloader
    dsampler = DistributedSampler(dset) if distributed else None
    dloader = DataLoader(dataset=dset,
                         batch_size=args.batch_size,
                         shuffle=(dsampler is None),
                         sampler=dsampler,
                         num_workers=args.n_workers,
                         pin_memory=True,
                         drop_last=True)

    ### MODEL
    N_CHANNEL = 3

    # Set DCGAN model