python numpy에서 HDF5을 다루는 방법 - LOPES-HUFS/DeepLearningFromForR GitHub Wiki

샘플 자료 만들기

class TwoLayerNet:

    def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01):
        # 가중치 초기화
        self.params = {}
        self.params['W1'] = weight_init_std * np.random.randn(input_size, hidden_size)
        self.params['b1'] = np.zeros(hidden_size)
        self.params['W2'] = weight_init_std * np.random.randn(hidden_size, output_size)
        self.params['b2'] = np.zeros(output_size)

net = TwoLayerNet(input_size=784, hidden_size=100, output_size=10)

저장하기

import h5py
hf = h5py.File('twoLayerNet.h5', 'w')
hf.create_dataset('W1',data=net.params['W1'], dtype=np.dtype('float64'))
hf.create_dataset('W2',data=net.params['W2'], dtype=np.dtype('float64'))
hf.create_dataset('b1',data=net.params['b1'], dtype=np.dtype('float64'))
hf.create_dataset('b2',data=net.params['b2'], dtype=np.dtype('float64'))
hf.close()

불러오기

hf = h5py.File('twoLayerNet.h5', 'r')
W1 = hf.get('W1')
W1
# 아직 파이썬에 입력되지 않은 상태
# 아래 코드로 입력
W1 = np.array(new_W1)
W1.shape
hf.close()