{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "92a1159e", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 1, "id": "6306fef2", "metadata": {}, "outputs": [], "source": [ "X = [1, 2, 3] #数据(3,)\n", "weight = [2, -1, 1] #权重\n", "bias = 2#偏置" ] }, { "cell_type": "markdown", "id": "c71760e7", "metadata": {}, "source": [ "转成ndarray" ] }, { "cell_type": "code", "execution_count": 4, "id": "6ad946d7", "metadata": {}, "outputs": [], "source": [ "X = np.array(X)\n", "weight = np.array(weight)\n", "bias = np.array(bias)" ] }, { "cell_type": "code", "execution_count": 5, "id": "ad1fd582", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((3,), (3,), ())" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.shape, weight.shape, bias.shape" ] }, { "cell_type": "markdown", "id": "1e81245b", "metadata": {}, "source": [ "向量点成" ] }, { "cell_type": "code", "execution_count": 7, "id": "795bd2f0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dot(X, weight), np.dot(weight, X)" ] }, { "cell_type": "code", "execution_count": 8, "id": "6c087a5a", "metadata": {}, "outputs": [], "source": [ "output = np.dot(X, weight) + bias" ] }, { "cell_type": "code", "execution_count": 9, "id": "6d64a1bc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "output" ] }, { "cell_type": "code", "execution_count": 10, "id": "6f7fc0e1", "metadata": {}, "outputs": [], "source": [ "X = [1, 2, 3] #数据(3,)\n", "weight = [[2, -1, 1],\n", " [2, 3, 1]] #权重\n", "bias = [2, 3]#偏置" ] }, { "cell_type": "code", "execution_count": 11, "id": "c4bbb7a6", "metadata": {}, "outputs": [], "source": [ "X = np.array(X)\n", "weight = np.array(weight)\n", "bias = np.array(bias)" ] }, { "cell_type": "code", "execution_count": 12, "id": "1ce4b03a", "metadata": {}, "outputs": [], "source": [ "output = np.dot(weight, X) + bias" ] }, { "cell_type": "code", "execution_count": 13, "id": "f76ed5a0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 5, 14])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "output" ] }, { "cell_type": "code", "execution_count": 21, "id": "d83e4284", "metadata": {}, "outputs": [], "source": [ "X = [[1, 2, 3],\n", " [2, 3, 4],\n", " [4, 5, 6]] #数据(3, 3)\n", "weight = [[2, -1, 1],\n", " [2, 3, 1]] #权重\n", "bias = [[2, 3]]#偏置" ] }, { "cell_type": "code", "execution_count": 22, "id": "75e715a9", "metadata": {}, "outputs": [], "source": [ "X = np.array(X)\n", "weight = np.array(weight)\n", "bias = np.array(bias)" ] }, { "cell_type": "markdown", "id": "c397fd32", "metadata": {}, "source": [ "对于多个数据,还是和上面使用同样的点乘是不行的。一定要注意数据的维度。" ] }, { "cell_type": "code", "execution_count": 30, "id": "ab7fe33a", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "shapes (3,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_1764\\268821979.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0moutput\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mweight\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mbias\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m<__array_function__ internals>\u001b[0m in \u001b[0;36mdot\u001b[1;34m(*args, **kwargs)\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: shapes (3,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)" ] } ], "source": [ "output = np.dot(X, weight) + bias" ] }, { "cell_type": "code", "execution_count": 31, "id": "844d2afe", "metadata": {}, "outputs": [], "source": [ "output = np.dot(X, weight.T) + bias" ] }, { "cell_type": "code", "execution_count": 32, "id": "5e945d68", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 5, 14],\n", " [ 7, 20],\n", " [11, 32]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "output" ] }, { "cell_type": "code", "execution_count": null, "id": "c71f101e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.7.9" } }, "nbformat": 4, "nbformat_minor": 5 }