Machine Learning Kurs im Rahmen der Studierendentage im SS 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.4 KiB

2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "id": "df1f5eb3",
  6. "metadata": {},
  7. "source": [
  8. "# demonstration of broadcasting in tensorflow"
  9. ]
  10. },
  11. {
  12. "cell_type": "code",
  13. "execution_count": null,
  14. "id": "1d61c70a",
  15. "metadata": {},
  16. "outputs": [],
  17. "source": [
  18. "import tensorflow as tf"
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": null,
  24. "id": "38bca1cf",
  25. "metadata": {},
  26. "outputs": [],
  27. "source": [
  28. "# Define two tensors with different shapes\n",
  29. "a = tf.constant([[1, 2, 3], [4, 5, 6]])\n",
  30. "b = tf.constant([10, 20, 30])"
  31. ]
  32. },
  33. {
  34. "cell_type": "code",
  35. "execution_count": null,
  36. "id": "c3f382e3",
  37. "metadata": {},
  38. "outputs": [],
  39. "source": [
  40. "# Perform element-wise multiplication using broadcasting\n",
  41. "c = a * b\n",
  42. "# Print the result\n",
  43. "print(c)\n"
  44. ]
  45. },
  46. {
  47. "cell_type": "code",
  48. "execution_count": null,
  49. "id": "95683fe5",
  50. "metadata": {},
  51. "outputs": [],
  52. "source": [
  53. "# Broadcasting scalar to tensor\n",
  54. "x = tf.constant([1, 2, 3])\n",
  55. "y = 2\n",
  56. "z = x + y # equivalent to tf.add(x, y)\n",
  57. "print(z.numpy()) # [3 4 5]"
  58. ]
  59. },
  60. {
  61. "cell_type": "code",
  62. "execution_count": null,
  63. "id": "8ed98565",
  64. "metadata": {},
  65. "outputs": [],
  66. "source": [
  67. "# Broadcasting vector to matrix\n",
  68. "x = tf.constant([[1, 2], [3, 4]])\n",
  69. "y = tf.constant([1, 2])\n",
  70. "z = x + y # equivalent to tf.add(x, y)\n",
  71. "print(z.numpy()) # [[2 4], [4 6]]"
  72. ]
  73. },
  74. {
  75. "cell_type": "code",
  76. "execution_count": null,
  77. "id": "41f4196f",
  78. "metadata": {},
  79. "outputs": [],
  80. "source": [
  81. "# Broadcasting matrix to tensor\n",
  82. "x = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])\n",
  83. "y = tf.constant([[1], [2]])\n",
  84. "z = x + y # equivalent to tf.add(x, y)\n",
  85. "print(z.numpy()) # [[[2 3], [4 5]], [[7 8], [9 10]]]"
  86. ]
  87. },
  88. {
  89. "cell_type": "code",
  90. "execution_count": null,
  91. "id": "76a5108d",
  92. "metadata": {},
  93. "outputs": [],
  94. "source": []
  95. }
  96. ],
  97. "metadata": {
  98. "kernelspec": {
  99. "display_name": "Python 3 (ipykernel)",
  100. "language": "python",
  101. "name": "python3"
  102. },
  103. "language_info": {
  104. "codemirror_mode": {
  105. "name": "ipython",
  106. "version": 3
  107. },
  108. "file_extension": ".py",
  109. "mimetype": "text/x-python",
  110. "name": "python",
  111. "nbconvert_exporter": "python",
  112. "pygments_lexer": "ipython3",
  113. "version": "3.8.16"
  114. }
  115. },
  116. "nbformat": 4,
  117. "nbformat_minor": 5
  118. }