def test_np_empty(): dtypes = [np.int8, np.int32, np.float16, np.float32, np.float64, None] expected_dtypes = [ np.int8, np.int32, np.float16, np.float32, np.float64, np.float32 ] orders = ['C', 'F', 'A'] shapes = [ (), 0, (0, ), (0, 0), 2, (2, ), (3, 0), (4, 5), (1, 1, 1, 1), ] ctxes = [npx.current_context(), None] for dtype, expected_dtype in zip(dtypes, expected_dtypes): for shape in shapes: for order in orders: for ctx in ctxes: if order == 'C': ret = np.empty(shape, dtype, order, ctx) assert ret.dtype == expected_dtype assert ret.shape == shape if isinstance( shape, tuple) else (shape, ) assert ret.ctx == npx.current_context() else: assert_exception(np.empty, NotImplementedError, shape, dtype, order, ctx)
def __init__(self, user_ids, item_ids, L, num_users, num_items, candidates): user_ids, item_ids = np.array(user_ids), np.array(item_ids) sort_idx = np.array(sorted(range(len(user_ids)), key=lambda k: user_ids[k])) u_ids, i_ids = user_ids[sort_idx], item_ids[sort_idx] temp, u_ids, self.cand = {}, u_ids.asnumpy(), candidates self.all_items = set([i for i in range(num_items)]) # 按照u_id-item编号,一个u的所有item完了,才下一个u的item编号 [temp.setdefault(u_ids[i], []).append(i) for i, _ in enumerate(u_ids)] temp = sorted(temp.items(), key=lambda x: x[0]) u_ids = np.array([i[0] for i in temp]) idx = np.array([i[1][0] for i in temp]) # ns是什么意思 self.ns = ns = int(sum([c - L if c >= L + 1 else 1 for c in np.array([len(i[1]) for i in temp])])) self.seq_items = np.zeros((ns, L)) self.seq_users = np.zeros(ns, dtype='int32') self.seq_tgt = np.zeros((ns, 1)) # target序列 self.test_seq = np.zeros((num_users, L)) test_users, _uid = np.empty(num_users), None for i, (uid, i_seq) in enumerate(self._seq(u_ids, i_ids, idx, L + 1)): # 最后一个序列为test集 if uid != _uid: # 这里也感觉不对 self.test_seq[uid][:] = i_seq[-L:] test_users[uid], _uid = uid, uid # 这里总感觉不对 self.seq_tgt[i][:] = i_seq[-1:] self.seq_items[i][:], self.seq_users[i] = i_seq[:L], uid
def get_conv_data_mxnet(oc, ic, n, k, p, s): mpx.random.seed(0) data = mp.random.normal(size=(1, ic, n, n)) weight = mp.random.normal(size=(oc, ic, k, k)) bias = mp.zeros((oc, )) on = conv_out_size(n, k, p, s) out = mp.empty((1, oc, on, on)) # Wait data are generated to make later benchmarking accurate mpx.waitall() return data, weight, bias, out
def test_np_empty(): # (input dtype, expected output dtype) dtype_pairs = [ (np.int8, np.int8), (np.int32, np.int32), (np.float16, np.float16), (np.float32, np.float32), (np.float64, np.float64), (np.bool_, np.bool_), (np.bool, np.bool_), ('int8', np.int8), ('int32', np.int32), ('float16', np.float16), ('float32', np.float32), ('float64', np.float64), ('bool', np.bool_), (None, np.float32), ] orders = ['C', 'F', 'A'] shapes = [ (), 0, (0, ), (0, 0), 2, (2, ), (3, 0), (4, 5), (1, 1, 1, 1), ] ctxes = [npx.current_context(), None] for dtype, expected_dtype in dtype_pairs: for shape in shapes: for order in orders: for ctx in ctxes: if order == 'C': ret = np.empty(shape, dtype, order, ctx) assert ret.dtype == expected_dtype assert ret.shape == shape if isinstance( shape, tuple) else (shape, ) assert ret.ctx == npx.current_context() else: assert_exception(np.empty, NotImplementedError, shape, dtype, order, ctx)
# vector 생성 x = np.arange(12) x x.shape # shape 확인 x.size # size 확인 # shape chahe x = x.reshape(3, 4) x x.shape # 자동으로 남은 값들의 dimention 작동 x.reshape(-1, 4) x.reshape(3, -1) # empty 메모리의 덩어리를 가져오는 mothod np.empty((3, 4)) # 0으로 값을 채우기 np.zeros((2, 3, 4)) # 1로 값을 채우기 np.ones((2, 3, 4)) # 평균이 0이고, 표준편차가 1인 (3, 4) dimention 만들기 np.random.normal(0, 1, size=(3, 4)) # 직접 리스트로 dimention 만들기 np.array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])