python神经网络包_手把手教你用Python创建简单的神经网络!

import numpy as np

class NeuralNetwork():

def init(self):

seeding for random number generation

np.random.seed(1)

converting weights to a 3 by 1 matrix with values from -1 to 1 and mean of 0

self.synaptic_weights = 2 * np.random.random((3,1)) - 1

def sigmoid(self,x):

applying the sigmoid function

return 1 / (1 + np.exp(-x))

def sigmoid_derivative(self,x):

computing derivative to the Sigmoid function

return x * (1 - x)

def train(self,training_inputs,training_outputs,training_iterations):

training the model to make accurate predictions while adjusting weights continually

for iteration in range(training_iterations):

siphon the training data via the neuron

output = self.think(training_inputs)

computing error rate for back-propagation

error = training_outputs - output

performing weight adjustments

adjustments = np.dot(training_inputs.T,error * self.sigmoid_derivative(output))

self.synaptic_weights += adjustments

def think(self,inputs):

passing the inputs via the neuron to get output

converting values to floats

inputs = inputs.astype(float)

output = self.sigmoid(np.dot(inputs,self.synaptic_weights))

return output

if name == "main":

initializing the neuron class

neural_network = NeuralNetwork()

print("Beginning Randomly Generated Weights: ")

print(neural_network.synaptic_weights)

training data consisting of 4 examples--3 input values and 1 output

training_inputs = np.array([[0,1],[1,1,[0,1]])

training_outputs = np.array([[0,0]]).T

training taking place

neural_network.train(training_inputs,15000)

print("Ending Weights After Training: ")

print(neural_network.synaptic_weights)

user_input_one = str(input("User Input One: "))

user_input_two = str(input("User Input Two: "))

user_input_three = str(input("User Input Three: "))

print("Considering New Situation: ",user_input_one,user_input_two,user_input_three)

print("New Output data: ")

print(neural_network.think(np.array([user_input_one,user_input_three])))

print("Wow,we did it!")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
神经网络是一种模拟人脑神经元之间相互连接的计算模型。在Python中,可以使用多种库来实现神经网络,如TensorFlow、PyTorch和Keras。下面是一个简单手把手教程来构建一个基本的神经网络模型: 1. 导入所需的库: ```python import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from tensorflow.keras.datasets import mnist from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.optimizers import Adam ``` 2. 加载数据集: ```python (x_train, y_train), (x_test, y_test) = mnist.load_data() ``` 3. 数据预处理: ```python # 将图像转换为一维向量 x_train = x_train.reshape(x_train.shape[0], -1) x_test = x_test.reshape(x_test.shape[0], -1) # 将图像像素值缩放到0-1之间 x_train = x_train.astype('float32') / 255.0 x_test = x_test.astype('float32') / 255.0 # 对标签进行独热编码 y_train = tf.keras.utils.to_categorical(y_train, num_classes=10) y_test = tf.keras.utils.to_categorical(y_test, num_classes=10) ``` 4. 构建模型: ```python model = Sequential() model.add(Dense(64, activation='relu', input_shape=(784,))) model.add(Dense(64, activation='relu')) model.add(Dense(10, activation='softmax')) ``` 5. 编译模型: ```python model.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy']) ``` 6. 训练模型: ```python history = model.fit(x_train, y_train, batch_size=128, epochs=10, validation_split=0.2) ``` 7. 评估模型性能: ```python loss, accuracy = model.evaluate(x_test, y_test) print('Test loss:', loss) print('Test accuracy:', accuracy) ``` 这是一个简单的示例,你可以根据自己的需求和数据集进行进一步的修改和调整。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值