machinelearning

Machine learning code
git clone git://git.laack.co/machinelearning.git
Log | Files | Refs

TensorsWithTF.ipynb (4454B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "code",
      5    "execution_count": 16,
      6    "metadata": {},
      7    "outputs": [
      8     {
      9      "data": {
     10       "text/plain": [
     11        "<tf.Tensor: shape=(2, 2), dtype=float32, numpy=\n",
     12        "array([[1., 2.],\n",
     13        "       [4., 5.]], dtype=float32)>"
     14       ]
     15      },
     16      "execution_count": 16,
     17      "metadata": {},
     18      "output_type": "execute_result"
     19     }
     20    ],
     21    "source": [
     22     "import tensorflow as tf\n",
     23     "\n",
     24     "t = tf.constant([[1.,2.], [4.,5]])\n",
     25     "t"
     26    ]
     27   },
     28   {
     29    "cell_type": "code",
     30    "execution_count": 17,
     31    "metadata": {},
     32    "outputs": [
     33     {
     34      "data": {
     35       "text/plain": [
     36        "(TensorShape([2, 2]), tf.float32)"
     37       ]
     38      },
     39      "execution_count": 17,
     40      "metadata": {},
     41      "output_type": "execute_result"
     42     }
     43    ],
     44    "source": [
     45     "t.shape, t.dtype"
     46    ]
     47   },
     48   {
     49    "cell_type": "code",
     50    "execution_count": 18,
     51    "metadata": {},
     52    "outputs": [
     53     {
     54      "data": {
     55       "text/plain": [
     56        "<tf.Tensor: shape=(2, 2), dtype=float32, numpy=\n",
     57        "array([[1., 4.],\n",
     58        "       [2., 5.]], dtype=float32)>"
     59       ]
     60      },
     61      "execution_count": 18,
     62      "metadata": {},
     63      "output_type": "execute_result"
     64     }
     65    ],
     66    "source": [
     67     "tf.transpose(t)"
     68    ]
     69   },
     70   {
     71    "cell_type": "code",
     72    "execution_count": 19,
     73    "metadata": {},
     74    "outputs": [
     75     {
     76      "data": {
     77       "text/plain": [
     78        "<tf.Tensor: shape=(2, 2), dtype=float32, numpy=\n",
     79        "array([[ 5., 14.],\n",
     80        "       [14., 41.]], dtype=float32)>"
     81       ]
     82      },
     83      "execution_count": 19,
     84      "metadata": {},
     85      "output_type": "execute_result"
     86     }
     87    ],
     88    "source": [
     89     "t @ tf.transpose(t)"
     90    ]
     91   },
     92   {
     93    "cell_type": "code",
     94    "execution_count": 20,
     95    "metadata": {},
     96    "outputs": [
     97     {
     98      "data": {
     99       "text/plain": [
    100        "<tf.Tensor: shape=(2, 2), dtype=float32, numpy=\n",
    101        "array([[-1.6666667 ,  0.6666667 ],\n",
    102        "       [ 1.3333334 , -0.33333334]], dtype=float32)>"
    103       ]
    104      },
    105      "execution_count": 20,
    106      "metadata": {},
    107      "output_type": "execute_result"
    108     }
    109    ],
    110    "source": [
    111     "tf.linalg.inv(t)"
    112    ]
    113   },
    114   {
    115    "cell_type": "code",
    116    "execution_count": 23,
    117    "metadata": {},
    118    "outputs": [
    119     {
    120      "data": {
    121       "text/plain": [
    122        "<tf.Tensor: shape=(), dtype=float32, numpy=12.0>"
    123       ]
    124      },
    125      "execution_count": 23,
    126      "metadata": {},
    127      "output_type": "execute_result"
    128     }
    129    ],
    130    "source": [
    131     "tf.reduce_sum(t)"
    132    ]
    133   },
    134   {
    135    "cell_type": "code",
    136    "execution_count": 27,
    137    "metadata": {},
    138    "outputs": [
    139     {
    140      "data": {
    141       "text/plain": [
    142        "<tf.Tensor: shape=(2,), dtype=float64, numpy=array([2., 4.])>"
    143       ]
    144      },
    145      "execution_count": 27,
    146      "metadata": {},
    147      "output_type": "execute_result"
    148     }
    149    ],
    150    "source": [
    151     "import numpy as np\n",
    152     "a = np.array([2.,4.])\n",
    153     "tf.constant(a)"
    154    ]
    155   },
    156   {
    157    "cell_type": "code",
    158    "execution_count": 29,
    159    "metadata": {},
    160    "outputs": [
    161     {
    162      "data": {
    163       "text/plain": [
    164        "array([[1., 2.],\n",
    165        "       [4., 5.]], dtype=float32)"
    166       ]
    167      },
    168      "execution_count": 29,
    169      "metadata": {},
    170      "output_type": "execute_result"
    171     }
    172    ],
    173    "source": [
    174     "t.numpy()"
    175    ]
    176   },
    177   {
    178    "cell_type": "code",
    179    "execution_count": 30,
    180    "metadata": {},
    181    "outputs": [
    182     {
    183      "data": {
    184       "text/plain": [
    185        "<tf.Tensor: shape=(2,), dtype=float64, numpy=array([ 4., 16.])>"
    186       ]
    187      },
    188      "execution_count": 30,
    189      "metadata": {},
    190      "output_type": "execute_result"
    191     }
    192    ],
    193    "source": [
    194     "tf.square(a)"
    195    ]
    196   },
    197   {
    198    "cell_type": "code",
    199    "execution_count": 31,
    200    "metadata": {},
    201    "outputs": [
    202     {
    203      "data": {
    204       "text/plain": [
    205        "array([[ 1.,  4.],\n",
    206        "       [16., 25.]], dtype=float32)"
    207       ]
    208      },
    209      "execution_count": 31,
    210      "metadata": {},
    211      "output_type": "execute_result"
    212     }
    213    ],
    214    "source": [
    215     "np.square(t)"
    216    ]
    217   }
    218  ],
    219  "metadata": {
    220   "kernelspec": {
    221    "display_name": ".venv",
    222    "language": "python",
    223    "name": "python3"
    224   },
    225   "language_info": {
    226    "codemirror_mode": {
    227     "name": "ipython",
    228     "version": 3
    229    },
    230    "file_extension": ".py",
    231    "mimetype": "text/x-python",
    232    "name": "python",
    233    "nbconvert_exporter": "python",
    234    "pygments_lexer": "ipython3",
    235    "version": "3.11.2"
    236   }
    237  },
    238  "nbformat": 4,
    239  "nbformat_minor": 2
    240 }