【Python-ML】神经网络激励函数-Softmax

# -*- coding: utf-8 -*-
'''
Created on 2018年1月27日
@author: Jason.F
@summary: 前馈神经网络激励函数-softmax函数,评估多类别分类任务中的类别概率
'''
import numpy as np
import time
       
if __name__ == "__main__":   
    start = time.clock()  
    
    def net_input(X,w):
        z=X.dot(w)
        return z
    def softmax(z):
        return np.exp(z)/np.sum(np.exp(z))
    def softmax_activation(X,w):
        z=net_input(X,w)
        return softmax(z)
    #W:array,shape=[n_output_units,n_hidden_units+1],weight matrix for hidden layer --> output layer
    #note that first column (A[:][0]=1) are the bias units.
    W=np.array([[1.1,1.2,1.3,0.5],[0.1,0.2,0.4,0.1],[0.2,0.5,2.1,1.9]])
    #A:array,shape=[n_hiddern+1,n_samples],Activation of hidden layer.
    #note that first element (A[0][0]=1) is the bias unit.
    A=np.array([[1.0],[0.1],[0.3],[0.7]])
    #Z:array,shape=[n_output_units,n_samples],Net input of the output layer.
    Z=W.dot(A)
    y_probas = softmax(Z)
    print ('Probabilities:\n',y_probas)
    print (y_probas.sum())
    y_class = np.argmax(Z,axis=0)
    print ('predicted class label:%d'%y_class[0])
    
    
    end = time.clock()    
    print('finish all in %s' % str(end - start)) 


结果:

('Probabilities:\n', array([[ 0.40386493],
       [ 0.07756222],
       [ 0.51857284]]))
1.0
predicted class label:2
finish all in 0.00170994801643


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CNN-GRU(Convolutional Neural Network - Gated Recurrent Unit)是一种深度学习模型,它结合了卷积神经网络(CNN)和门控循环单元(GRU)。CNN主要用于处理图像数据,提取局部特征,而GRU是一种递归神经网络(RNN)的一种变体,特别适合处理序列数据,如文本、音频等。 在Python中,常用的库如TensorFlow和Keras可以方便地构建这种结构。以下是基本步骤: 1. **导入所需库**:首先需要import tensorflow、keras以及相关的层模块,例如`tensorflow.keras.layers.Conv2D`(用于创建卷积层)和`tensorflow.keras.layers.GRU`(用于创建GRU层)。 ```python import tensorflow as tf from tensorflow.keras import layers ``` 2. **定义模型**:创建一个Sequential模型,并添加一个或多个卷积层来捕获空间特征,接着添加一个GRU层作为序列处理部分。 ```python model = tf.keras.Sequential([ layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', input_shape=(image_width, image_height, channels)), # 其他可能的卷积层... layers.GRU(units=64, return_sequences=True) # 如果输入是时间序列,return_sequences=True ]) ``` 3. **连接全连接层和输出层**:根据任务需求,可能还需要添加一些全连接层和适当的激活函数(如sigmoid或softmax),最后加上一个输出层。 ```python model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(num_classes, activation='softmax')) # 对于分类问题 ``` 4. **编译模型**:设置损失函数、优化器和评估指标。 ```python model.compile(optimizer=tf.optimizers.Adam(), loss='categorical_crossentropy', metrics=['accuracy']) ``` 5. **训练模型**:提供训练数据和标签,调用`fit()`函数训练模型。 ```python history = model.fit(X_train, y_train, epochs=num_epochs, validation_data=(X_val, y_val)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值