Aimbot-PPO/Aimbot-PPO-Python/Tensorflow/testarea.ipynb
Koha9 742529ccd7 Archive all tensorflow agents and env
archive all TF py&ipynb
turn face to pytorch.
2022-10-26 03:15:37 +09:00

616 lines
16 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 7, 8, 9, 10]])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"state = np.array([[1, 2, 3], [1, 2, 3]])\n",
"aaa = np.array([[123]])\n",
"\n",
"state[:, -1]\n",
"\n",
"np.append([[1, 2, 3]], [[7, 8, 9, 10]], axis=1)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import numpy as np\n",
"\n",
"aa = tf.constant([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]])\n",
"bb = tf.constant([1, 2, 3, 4])\n",
"\n",
"print(tf.expand_dims(bb, axis=1))\n",
"\n",
"cc = tf.math.multiply(aa, tf.expand_dims(bb, axis=1))\n",
"\n",
"print(cc)\n",
"print(tf.shape(aa))\n",
"\n",
"print(aa[:, 2:3])\n",
"\n",
"aa = tf.constant([1.0, 2.0, 3.0, np.nan])\n",
"print(np.any(tf.math.is_nan(aa)))\n",
"if np.any(tf.math.is_nan(aa)):\n",
" print(\"true\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"\n",
"prob = tf.constant([0.3, 0.3, 0.0])\n",
"\n",
"entropy = tf.reduce_mean(\n",
" tf.math.negative(tf.math.multiply(prob, tf.math.log(prob)))\n",
")\n",
"\n",
"print(entropy)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"int 23\n",
"str twenty three\n",
"bool True\n",
"error\n"
]
}
],
"source": [
"from functools import singledispatchmethod\n",
"\n",
"\n",
"class person:\n",
" @singledispatchmethod\n",
" def age(self, arg):\n",
" print(\"error\")\n",
"\n",
" @age.register(int)\n",
" def _(self, arg: int):\n",
" print(\"int\", arg)\n",
"\n",
" @age.register(str)\n",
" def _(self, arg: str):\n",
" print(\"str\", arg)\n",
"\n",
" @age.register(bool)\n",
" def _(self, arg: bool):\n",
" print(\"bool\", arg)\n",
"\n",
"\n",
"p = person()\n",
"p.age(23) # int\n",
"p.age(\"twenty three\") # str\n",
"p.age(True) # bool\n",
"p.age([\"23\"]) # list\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import tensorflow.keras.layers as layers\n",
"\n",
"\n",
"def conv_bn_relu(inputs, chs, reps):\n",
" x = inputs\n",
" for i in range(reps):\n",
" x = layers.Conv2D(chs, 3, padding=\"same\")(x)\n",
" x = layers.BatchNormalization()(x)\n",
" x = layers.ReLU()(x)\n",
" return x\n",
"\n",
"\n",
"def create_model():\n",
" inputs = layers.Input((32, 32, 3))\n",
" x = conv_bn_relu(inputs, 64, 3)\n",
" x = layers.AveragePooling2D(2)(x)\n",
" x = conv_bn_relu(x, 128, 3)\n",
" x = layers.AveragePooling2D(2)(x)\n",
" x = conv_bn_relu(x, 256, 3)\n",
" x = layers.GlobalAveragePooling2D()(x)\n",
" x = layers.Dense(10, activation=\"softmax\")(x)\n",
" return tf.keras.models.Model(inputs, x)\n",
"\n",
"\n",
"def perprocess(img, label):\n",
" img = tf.cast(img, tf.float32) / 255.0\n",
" label = tf.cast(label, tf.float32)\n",
" return img, label\n",
"\n",
"\n",
"def train():\n",
" (X_train, y_train), (\n",
" X_test,\n",
" y_test,\n",
" ) = tf.keras.datasets.cifar10.load_data()\n",
" trainset = (\n",
" tf.data.Dataset.from_tensor_slices((X_train, y_train))\n",
" .map(perprocess)\n",
" .shuffle(4096)\n",
" .batch(128)\n",
" .repeat()\n",
" .prefetch(50)\n",
" )\n",
"\n",
" model = create_model()\n",
" model.compile(\"adam\", \"sparse_categorical_crossentropy\", [\"acc\"])\n",
"\n",
" model.fit(trainset, steps_per_epoch=50000 // 128, epochs=1)\n",
" # 'Adam/conv2d/kernel/m:0' shape=(3, 3, 3, 64)\n",
" print(model.optimizer.weights[1][0, 0, 0, :10])\n",
" # <tf.Variable 'conv2d/kernel:0' shape=(3, 3, 3, 64)\n",
" print(model.weights[0][0, 0, 0, :10])\n",
"\n",
" model.save_weights(\"model_tf.ckpt\", save_format=\"tf\") # デフォルト\n",
" model.save_weights(\"model_h5.h5\", save_format=\"h5\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"390/390 [==============================] - 28s 59ms/step - loss: 1.2548 - acc: 0.5441\n",
"tf.Tensor(\n",
"[ 0.00190057 -0.00765918 0.00163367 0.00782851 0.02600338 0.00516749\n",
" -0.00424899 0.01562062 -0.0022073 -0.00355565], shape=(10,), dtype=float32)\n",
"tf.Tensor(\n",
"[ 0.07978954 -0.04595745 -0.03745254 -0.03701654 0.03296526 -0.11328737\n",
" -0.10719797 0.00874998 0.0226855 0.02288487], shape=(10,), dtype=float32)\n"
]
}
],
"source": [
"train()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[ 0.07978954 -0.04595745 -0.03745254 -0.03701654 0.03296526 -0.11328737\n",
" -0.10719797 0.00874998 0.0226855 0.02288487], shape=(10,), dtype=float32)\n",
"tf.Tensor(\n",
"[ 0.00190057 -0.00765918 0.00163367 0.00782851 0.02600338 0.00516749\n",
" -0.00424899 0.01562062 -0.0022073 -0.00355565], shape=(10,), dtype=float32)\n"
]
}
],
"source": [
"def load_tf_w_zero_grad():\n",
" model = create_model()\n",
" model.compile(\"adam\", \"sparse_categorical_crossentropy\", [\"acc\"])\n",
"\n",
" zero_grad = [tf.zeros_like(x) for x in model.weights]\n",
" model.optimizer.apply_gradients(zip(zero_grad, model.weights))\n",
"\n",
" model.load_weights(\"model_tf.ckpt\")\n",
" # これでようやくオプティマイザーの値も同一になる\n",
" print(model.weights[0][0, 0, 0, :10])\n",
" print(model.optimizer.weights[1][0, 0, 0, :10])\n",
"\n",
"\n",
"load_tf_w_zero_grad()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"a = np.array([10, 20, 30, 0])\n",
"\n",
"np.asarray([[0.]])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.5"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"asd = [1,2,3,np.array([0.5]),np.array([0.5])]\n",
"\n",
"asd[3:]\n",
"len(asd)\n",
"\n",
"np.mean([1,2])"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0\n",
"0.0\n"
]
}
],
"source": [
"import time\n",
"import pyautogui as pag\n",
"\n",
"from pynput.mouse import Button, Controller\n",
"\n",
"w = pag.size().width\n",
"h = pag.size().height\n",
"mouse = Controller()\n",
"\n",
"nowt = time.time()\n",
"\n",
"middletime = time.time() - nowt\n",
"print(middletime)\n",
"# print(nowPos-(w/2))\n",
"\n",
"print(time.time() - middletime - nowt)\n",
"while True:\n",
" x,_ = mouse.position\n",
" #print(mouse.press)\n",
" #print(mouse.position)\n",
" \n",
" mouse.position = (w / 2, h / 2)\n",
" time.sleep(1/60)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import pyautogui as pag\n",
"\n",
"import mouse\n",
"\n",
"w = pag.size().width\n",
"h = pag.size().height\n",
"\n",
"nowt = time.time()\n",
"\n",
"middletime = time.time() - nowt\n",
"print(middletime)\n",
"# print(nowPos-(w/2))\n",
"\n",
"print(time.time() - middletime - nowt)\n",
"while True:\n",
" x = mouse.get_position()\n",
" print(x)\n",
" #print(mouse.position)\n",
" \n",
" mouse.move(w / 2, h / 2)\n",
" time.sleep(1/60)\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"deque([[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0], [1, 1, 1, 1, 1]], maxlen=3)\n",
"3\n",
"deque([[0.0, 0.0, 0.0, 0.0, 0.0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2]], maxlen=3)\n",
"3\n",
"deque([[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3]], maxlen=3)\n",
"3\n",
"deque([[2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4]], maxlen=3)\n",
"3\n",
"deque([[3, 3, 3, 3, 3], [4, 4, 4, 4, 4], [5, 5, 5, 5, 5]], maxlen=3)\n",
"3\n",
"deque([[4, 4, 4, 4, 4], [5, 5, 5, 5, 5], [6, 6, 6, 6, 6]], maxlen=3)\n",
"3\n",
"deque([[5, 5, 5, 5, 5], [6, 6, 6, 6, 6], [7, 7, 7, 7, 7]], maxlen=3)\n",
"3\n",
"deque([[6, 6, 6, 6, 6], [7, 7, 7, 7, 7], [8, 8, 8, 8, 8]], maxlen=3)\n",
"3\n",
"deque([[7, 7, 7, 7, 7], [8, 8, 8, 8, 8], [9, 9, 9, 9, 9]], maxlen=3)\n"
]
}
],
"source": [
"from collections import deque\n",
"import numpy as np\n",
"\n",
"maxBuffer = 3\n",
"stateSize = 5\n",
"\n",
"aa = deque([[0.0]*stateSize],maxlen=maxBuffer)\n",
"\n",
"def ss(s):\n",
" aa.append(s)\n",
" if len(aa) < maxBuffer:\n",
" for i in range(maxBuffer - len(aa)):\n",
" aa.appendleft([0.0] * stateSize)\n",
"\n",
"for i in range(1,10):\n",
" ss([i,i,i,i,i])\n",
" print(len(aa))\n",
" print(aa)\n",
"'''\n",
"3\n",
"deque([[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0], [1, 1, 1, 1, 1]], maxlen=3)\n",
"3\n",
"deque([[0.0, 0.0, 0.0, 0.0, 0.0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2]], maxlen=3)\n",
"3\n",
"deque([[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3]], maxlen=3)\n",
"3\n",
"deque([[2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4]], maxlen=3)\n",
"3\n",
"deque([[3, 3, 3, 3, 3], [4, 4, 4, 4, 4], [5, 5, 5, 5, 5]], maxlen=3)\n",
"3\n",
"deque([[4, 4, 4, 4, 4], [5, 5, 5, 5, 5], [6, 6, 6, 6, 6]], maxlen=3)\n",
"3\n",
"deque([[5, 5, 5, 5, 5], [6, 6, 6, 6, 6], [7, 7, 7, 7, 7]], maxlen=3)\n",
"3\n",
"deque([[6, 6, 6, 6, 6], [7, 7, 7, 7, 7], [8, 8, 8, 8, 8]], maxlen=3)\n",
"3\n",
"deque([[7, 7, 7, 7, 7], [8, 8, 8, 8, 8], [9, 9, 9, 9, 9]], maxlen=3)'''"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 0, 1, 2, 0, 1, 2])"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from collections import deque\n",
"import numpy as np\n",
"\n",
"aa = np.array([range(0,3)]*5)\n",
"np.reshape(aa[[0,1,2]],(9))"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'>\n",
"<class 'float'>\n",
"<class 'list'>\n",
"300\n",
"256.1\n",
"[300, 256.1]\n",
"300\n",
"256.1\n"
]
}
],
"source": [
"# 変数を設定\n",
"ringo_int = 300\n",
"ringo_float = 256.1\n",
"ringo_list = [ringo_int, ringo_float]\n",
"\n",
"# 型を確認\n",
"print(type(ringo_int))\n",
"print(type(ringo_float))\n",
"print(type(ringo_list))\n",
"\n",
"# 値を表示\n",
"print(ringo_int)\n",
"print(ringo_float)\n",
"print(ringo_list)\n",
"\n",
"# 配列から要素を取り出す\n",
"print(ringo_list[0]) # ここでエラーになるという。どぼじで???\n",
"print(ringo_list[1])"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"dirrr = \"GAIL-Expert-Data/1014-1302/pack-24957.npz\"\n",
"\n",
"memFile = np.load(dirrr, allow_pickle=True)\n",
"states = memFile[\"states\"].tolist()\n",
"actorProbs = memFile[\"actorProbs\"].tolist()\n",
"actions = memFile[\"actions\"].tolist()\n",
"rewards = memFile[\"rewards\"].tolist()\n",
"dones = memFile[\"dones\"].tolist()\n",
"memNum = len(states)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\UCUNI\\AppData\\Local\\Temp/ipykernel_39608/3742051961.py:3: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n",
" npact = np.array(actions)\n"
]
}
],
"source": [
"states = np.reshape(states, (24957, 90))\n",
"\n",
"npact = np.array(actions)\n",
"\n",
"last = npact[:,3]\n",
"newlast = []\n",
"last[2][0]\n",
"for i in range(len(last)):\n",
" newlast.append(last[i][0])\n",
"\n",
"#print(newlast)\n",
"npact[:,3] = newlast\n",
"\n",
"statesNP = np.asarray(states)\n",
"actorProbsNP = np.asarray(actorProbs)\n",
"actionsNP = np.asarray(npact)\n",
"rewardsNP = np.asarray(rewards)\n",
"donesNP = np.asarray(dones)\n",
"thisSaveDir = \"GAIL-Expert-Data/1014-1302/pack-24957-RE.npz\"\n",
"\n",
"np.savez(\n",
" thisSaveDir,\n",
" states=statesNP,\n",
" actorProbs=actorProbsNP,\n",
" actions=actionsNP,\n",
" rewards=rewardsNP,\n",
" dones=donesNP,\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(actions)\n",
"npact = np.array(actions)\n",
"\n",
"last = npact[:,3]\n",
"newlast = []\n",
"last[2][0]\n",
"for i in range(len(last)):\n",
" newlast.append(last[i][0])\n",
"\n",
"#print(newlast)\n",
"npact[:,3] = newlast\n",
"print(npact)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.7 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "86e2db13b09bd6be22cb599ea60c1572b9ef36ebeaa27a4c8e961d6df315ac32"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}