{ "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": 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\")\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": 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", "\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": 1, "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", " #