292 lines
7.5 KiB
Plaintext
292 lines
7.5 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)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"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')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import tensorflow as tf\n",
|
|
"\n",
|
|
"prob = tf.constant([0.3,0.3,0.])\n",
|
|
"\n",
|
|
"entropy = tf.reduce_mean(tf.math.negative(tf.math.multiply(prob,tf.math.log(prob))))\n",
|
|
"\n",
|
|
"print(entropy)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"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",
|
|
"class person:\n",
|
|
" @singledispatchmethod\n",
|
|
" def age(self,arg):\n",
|
|
" print(\"error\")\n",
|
|
" @age.register(int)\n",
|
|
" def _(self,arg:int):\n",
|
|
" print(\"int\",arg)\n",
|
|
" @age.register(str)\n",
|
|
" def _(self,arg:str):\n",
|
|
" print(\"str\",arg)\n",
|
|
" @age.register(bool)\n",
|
|
" def _(self,arg:bool):\n",
|
|
" print(\"bool\",arg)\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": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import tensorflow as tf\n",
|
|
"import tensorflow.keras.layers as layers\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",
|
|
"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",
|
|
"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",
|
|
"def train():\n",
|
|
" (X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()\n",
|
|
" trainset = tf.data.Dataset.from_tensor_slices((X_train, y_train)\n",
|
|
" ).map(perprocess).shuffle(4096).batch(128).repeat().prefetch(50)\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\") "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"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()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"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",
|
|
"load_tf_w_zero_grad()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 20,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"True"
|
|
]
|
|
},
|
|
"execution_count": 20,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"a = np.array([10,20,30,0])\n",
|
|
"\n",
|
|
"np.any(a == 0)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 29,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"4"
|
|
]
|
|
},
|
|
"execution_count": 29,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"asd = \"adsf\"\n",
|
|
"len(asd)"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|