cs231n homework 两层全连接神经网络 分类CIFAR-10

本文介绍了在cs231n课程作业中,使用两层全连接神经网络对CIFAR-10数据集进行分类的实践过程。虽然代码已完成,但仍有超参数调整及验证等步骤待完善,通常需要通过交叉验证选择最佳超参数组合。
摘要由CSDN通过智能技术生成

cs231n homework 两层全连接神经网络 分类CIFAR-10

code

import numpy as np
import pickle
import matplotlib.pyplot as plt


def loadfile():
    train_data = []
    train_label = []
    val_data = []
    val_label = []
    test_data = []
    test_label = []
    for i in range(1, 6):
        train_file = open('F:\PycharmProjects\cs231n\\two_layer_neuralnet\cifar-10-batches-py\data_batch_'+str(i),'rb')
        train_file_object = pickle.load(train_file,encoding='bytes')
        for line in train_file_object[b'data']:
            train_data.append(line)
        for line in train_file_object[b'labels']:
            train_label.append(line)
    train_data = np.array(train_data)
    train_label = np.array(train_label)
    subtraintoval = np.random.choice(train_data.shape[0],int(0.1*train_data.shape[0]))
    print(len(subtraintoval))
    val_data = train_data[subtraintoval].astype("float")
    val_label = train_label[subtraintoval]
    train_data = np.delete(train_data,subtraintoval,0)
    train_label = np.delete(train_label,subtraintoval,0)
    train_data = train_data.astype("float")
    test_file = open('F:\PycharmProjects\cs231n\\two_layer_neuralnet\cifar-10-batches-py\\test_batch', 'rb')
    test_file_object = pickle.load(test_file, encoding='bytes')
    # print(test_file_object)
    for line in test_file_object[b'data']:
        test_data.append(line)
    for line in test_file_object[b'labels']:
        test_label.append(line)
    test_data = np.array(test_data).astype("float")
    test_label = np.array(test_label)
    print("train_data  shape:" + str(train_data.shape))
    print("train_label shape:" + str(train_label.shape))
    print("val_data    shape:" + str(val_data.shape))
    print("val_label   shape:" + str(val_label.shape))
    print("test_data   shape:" + str(test_data.shape))
    print("test_label  shape:" + str(test_label.shape))
    return train_data,train_label,val_data,val_label,test_data,test_label


class TwoLayerNet(object):
    """
    A two-layer fully-connected neural network with ReLU nonlinearity and
    softmax loss that uses a modular layer design. We assume an input dimension
    of D, a hidden dimension of H, and perform classification over C classes.

    The architecure should be affine - relu - affine - softmax.

    Note that this class does not implement gradient descent; instead, it
    will interact with a separate Solver object that is responsible for running
    optimization.

    The learnable parameters of the model are stored in the dictionary
    self.params that maps parameter names to numpy arrays.
    """

    def __init__(self, input_dim=3 * 32 * 32, hidden_dim=100, num_classes=10,
                 weight_scale=1e-3, reg=0.0):
        """
        Initialize a new network.

        Inputs:
        - input_dim: An integer giving the size of the input
        - hidden_dim: An integer giving the size of the hidden layer
        - num_classes: A
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值