Skip to content

Instantly share code, notes, and snippets.

@marcrasi
Created November 14, 2019 22:22
Show Gist options
  • Save marcrasi/ff1ce3ff8e5beb82d0c4c87c9018336a to your computer and use it in GitHub Desktop.
Save marcrasi/ff1ce3ff8e5beb82d0c4c87c9018336a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import TensorFlow\n",
"var weights = Tensor<Float>(randomNormal: [784, 10]) / sqrt(784)\n",
"print(weights[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import Python\n",
"let np = Python.import(\"numpy\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"var bias = Tensor<Float>(zeros: [10])\n",
"\n",
"let m1 = Tensor<Float>(randomNormal: [5, 784])\n",
"let m2 = Tensor<Float>(randomNormal: [784, 10])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"m1: \", m1.shape)\n",
"print(\"m2: \", m2.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"let small = Tensor<Float>([[1, 2],\n",
" [3, 4]])\n",
"\n",
"print(\"🔢2x2:\\n\", small)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"⊞ matmul:\\n\", matmul(small, small))\n",
"print(\"\\n⊞ again:\\n\", small • small)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"var m = Tensor([1.0, 2, 3, 4, 5, 6, 7, 8, 9]).reshaped(to: [3, 3])\n",
"print(m)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sqrt((m * m).sum())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"var a = Tensor([10.0, 6, -4])\n",
"var b = Tensor([2.0, 8, 7])\n",
"(a,b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"add: \", a + b)\n",
"print(\"mul: \", a * b)\n",
"print(\"sqrt: \", sqrt(a))\n",
"print(\"pow: \", pow(a, b))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a .< b"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print((a .> 0).all())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print((a .> 0).any())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"var a = Tensor([10.0, 6.0, -4.0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(a+1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"2 * m"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"let c = Tensor([10.0,20.0,30.0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m + c"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c + m"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m + c.expandingShape(at: 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c.expandingShape(at: 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Swift",
"language": "swift",
"name": "swift"
},
"language_info": {
"file_extension": ".swift",
"mimetype": "text/x-swift",
"name": "swift",
"version": ""
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment