這是一個剪刀石頭布預測模型,會根據最近20局的歷史資料訓練模型,神經網路輸入為最近2局的歷史資料。
tensor = torch.tensor([1,2,3])
np_array = tensor.numpy()
// 方式一:arraySync() let tensor = tf.tensor1d([1,2,3]); let array = tensor.arraySync(); console.log(array); // [1,2,3] // 方式二:在async函數體內操作 async function fun() { let tensor = tf.tensor1d([1,2,3]); let array = await tensor.array(); console.log(array); // [1,2,3] } fun(); // 注意,下面的寫法是不行的,因為async函數的返回值是Promise物件 array = async function (){ return await tensor.array(); }(); console.log(array); // Promise object // 方式三:用then取出async函數返回Promise物件中的值 let a (async function() { let array = await tensor.array(); return array })().then(data => {a = data;}) console.log(a); // [1,2,3]
tensor = torch.tensor([1,2,3]) print(tensor[0]) print(tensor[-1])
const tensor = tf.tensor1d([1,2,3]); const array = tensor.arraySync(); console.log(array[0]);
console.log(array[array.length - 1]);
actions = {'up':[1,0,0,0], 'down':[0,1,0,0], 'left':[0,0,1,0], 'right':[0,0,0,1]} actions_keys_list = list(actions.keys())
const actions = {'up':[1,0,0,0], 'down':[0,1,0,0], 'left':[0,0,1,0], 'right':[0,0,0,1]};
const actionsKeysArray = Object.keys(actions);
memory = [1,2,3] memory.append(4) # 入棧 memory.pop(0) # 出棧
let memory = [1,2,3]; memory.push(4); // 入棧 memory.splice(0,1); // 出棧
memory = [1,2,3] memory.append(4) # 入棧 memory.pop() # 出棧
let memory = [1,2,3]; memory.push(4); // 入棧 memory.pop(); // 出棧
actions = ['up','down','left','right'] prob = [0.1, 0.4, 0.4, 0.1] sample_action = np.random.choice(actions, p=prob))
const actions = ['up', 'down', 'left', 'right']; const prob = [0.1, 0.4, 0.4, 0.1]; sampleActionIndex = tf.multinomial(prob, 1, null, true).arraySync(); // tf.Tensor 不能作為索引,需要用 arraySync() 同步地傳輸為 array sampleAction = actions[sampleActionIndex];
actions = ['up', 'down', 'left', 'right'] prob = [0.1, 0.3, 0.5, 0.1] prob_tensor = torch.tensor(prob) action_max_prob = actions[np.array(prob).argmax()] # np.array 可以作為索引 action_max_prob = actions[prob_tensor.argmax().numpy()] # torch.tensor 不能作為索引,需要轉換為 np.array
const actions = ['up', 'down', 'left', 'right']; const prob = [0.1, 0.3, 0.5, 0.1]; const probTensor = tf.tensor1d(prob); const actionsMaxProb = actions[probTensor.argmax().arraySync()]; // tf.Tensor 不能作為索引,需要用 arraySync()同步地傳輸為 array
range_list = list(range(1,10,1))
const rangeArray = tf.range(1, 10, 1).arraySync();
actions = ['up', 'down', 'left', 'right'] print(random.shuffle(actions))
const actions = ['up', 'down', 'left', 'right'];
tf.util.shuffle(actions);
console.log(actions);
(2)用 tf.data.shuffle 操作,不建議,該類及其方法一般僅與 神經網路模型更新 繫結使用。
import numpy as np import torch from torch import nn import random class Memory(object): # 向Memory輸送的資料可以是list,也可以是np.array def __init__(self, size=100, batch_size=32): self.memory_size = size self.batch_size = batch_size self.main = [] def save(self, data): if len(self.main) == self.memory_size: self.main.pop(0) self.main.append(data) def sample(self): samples = random.sample(self.main, self.batch_size) return map(np.array, zip(*samples)) class Model(object): # Model中所有方法的輸入和返回都是np.array def __init__(self, lr=0.01, device=None): self.LR = lr self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # 呼叫GPU 若無則CPU self.network = nn.Sequential(nn.Flatten(), nn.Linear(10, 32), nn.ReLU(), nn.Linear(32, 5), nn.Softmax(dim=1)).to(self.device) self.loss = nn.CrossEntropyLoss(reduction='mean') self.optimizer = torch.optim.Adam(self.network.parameters(), lr=self.LR) def predict_nograd(self, _input): with torch.no_grad(): _input = np.expand_dims(_input, axis=0) _input = torch.from_numpy(_input).float().to(self.device) _output = self.network(_input).cpu().numpy() _output = np.squeeze(_output) return _output def update(self, input_batch, target_batch): # 設定為訓練模式 self.network.train() _input_batch = torch.from_numpy(input_batch).float().to(self.device) _target_batch = torch.from_numpy(target_batch).float().to(self.device) self.optimizer.zero_grad() _evaluate_batch = self.network(_input_batch) batch_loss = self.loss(_evaluate_batch, _target_batch) batch_loss.backward() self.optimizer.step() batch_loss = batch_loss.item() # 設定為預測模式 self.network.eval() if __name__ == '__main__': memory = Memory() model = Model() # 產生資料並輸送到記憶體中 # 假設一個5分類問題 for i in range(memory.memory_size): example = np.random.randint(0,2,size=10) label = np.eye(5)[np.random.randint(0,5)] data = [example, label] memory.save(data) # 訓練100次,每次從記憶體中隨機抽取一個batch的資料 for i in range(100): input_batch, target_batch = memory.sample() model.update(input_batch, target_batch) # 預測 prediction = model.predict_nograd(np.random.randint(0,2,size=10)) print(prediction)
const Memory = { memorySize : 100, main : [], saveData : function (data) { // data = [input:array, label:array] if (this.main.length == this.memorySize) { this.main.splice(0,1); } this.main.push(data); }, getMemoryTensor: function () { let inputArray = [], labelArray = []; for (let i = 0; i < this.main.length; i++) { inputArray.push(this.main[i][0]) labelArray.push(this.main[i][1]) } return { inputBatch: tf.tensor2d(inputArray), labelBatch: tf.tensor2d(labelArray) } } } const Model = { batchSize: 32, epoch: 200, network: tf.sequential({ layers: [ tf.layers.dense({inputShape: [10], units: 16, activation: 'relu'}), tf.layers.dense({units: 5, activation: 'softmax'}), ] }), compile: function () { this.network.compile({ optimizer: tf.train.sgd(0.1), shuffle: true, loss: 'categoricalCrossentropy', metrics: ['accuracy'] }); }, predict: function (input) { // input = array // Return tensor1d return this.network.predict(tf.tensor2d([input])).squeeze(); }, update: async function (inputBatch, labelBatch) { // inputBatch = tf.tensor2d(memorySize × 10) // labelBatch = tf.tensor2d(memorySize × 5) this.compile(); await this.network.fit(inputBatch, labelBatch, { epochs: this.epoch, batchSize: this.batchSize }).then(info => { console.log('Final accuracy', info.history.acc); }); } } // 假設一個5分類問題 // 隨機生成樣例和標籤,並填滿記憶體 let example, label, rnd, data; for (let i = 0; i < Memory.memorySize; i++) { example = tf.multinomial(tf.tensor1d([.5, .5]), 10).arraySync(); rnd = Math.floor(Math.random()*5); label = tf.oneHot(tf.tensor1d([rnd], 'int32'), 5).squeeze().arraySync(); data = [example, label]; Memory.saveData(data); } // 將記憶體中儲存的資料匯出為tensor,並訓練模型 let {inputBatch, labelBatch} = Memory.getMemoryTensor(); Model.update(inputBatch, labelBatch);