tanh函数python tanh函数的反函数

tanh是什么函数

原型:extern float tanh(float x);

成都创新互联主营蓟州网站建设的网络公司,主营网站建设方案,app软件开发公司,蓟州h5成都小程序开发搭建,蓟州网站营销推广欢迎蓟州等地区企业咨询

用法:#include math.h

功能:求x的双曲正切值

说明:tanh(x)=(e^x-e^(-x))/(e^2+e^(-x))

举例:

// tanh.c

#include syslib.h

#include math.h

main()

{

float x;

clrscr(); // clear screen

textmode(0x00); // 6 lines per LCD screen

x=PI/4.;

printf("tanh(%.4f)=%.4f\n",x,tanh(x));

getchar();

return 0;

}

Python--math库

Python math 库提供许多对浮点数的数学运算函数,math模块不支持复数运算,若需计算复数,可使用cmath模块(本文不赘述)。

使用dir函数,查看math库中包含的所有内容:

1) math.pi    # 圆周率π

2) math.e    #自然对数底数

3) math.inf    #正无穷大∞,-math.inf    #负无穷大-∞

4) math.nan    #非浮点数标记,NaN(not a number)

1) math.fabs(x)    #表示X值的绝对值

2) math.fmod(x,y)    #表示x/y的余数,结果为浮点数

3) math.fsum([x,y,z])    #对括号内每个元素求和,其值为浮点数

4) math.ceil(x)    #向上取整,返回不小于x的最小整数

5)math.floor(x)    #向下取整,返回不大于x的最大整数

6) math.factorial(x)    #表示X的阶乘,其中X值必须为整型,否则报错

7) math.gcd(a,b)    #表示a,b的最大公约数

8)  math.frexp(x)      #x = i *2^j,返回(i,j)

9) math.ldexp(x,i)    #返回x*2^i的运算值,为math.frexp(x)函数的反运算

10) math.modf(x)    #表示x的小数和整数部分

11) math.trunc(x)    #表示x值的整数部分

12) math.copysign(x,y)    #表示用数值y的正负号,替换x值的正负号

13) math.isclose(a,b,rel_tol =x,abs_tol = y)    #表示a,b的相似性,真值返回True,否则False;rel_tol是相对公差:表示a,b之间允许的最大差值,abs_tol是最小绝对公差,对比较接近于0有用,abs_tol必须至少为0。

14) math.isfinite(x)    #表示当x不为无穷大时,返回True,否则返回False

15) math.isinf(x)    #当x为±∞时,返回True,否则返回False

16) math.isnan(x)    #当x是NaN,返回True,否则返回False

1) math.pow(x,y)    #表示x的y次幂

2) math.exp(x)    #表示e的x次幂

3) math.expm1(x)    #表示e的x次幂减1

4) math.sqrt(x)    #表示x的平方根

5) math.log(x,base)    #表示x的对数值,仅输入x值时,表示ln(x)函数

6) math.log1p(x)    #表示1+x的自然对数值

7) math.log2(x)    #表示以2为底的x对数值

8) math.log10(x)    #表示以10为底的x的对数值

1) math.degrees(x)    #表示弧度值转角度值

2) math.radians(x)    #表示角度值转弧度值

3) math.hypot(x,y)    #表示(x,y)坐标到原点(0,0)的距离

4) math.sin(x)    #表示x的正弦函数值

5) math.cos(x)    #表示x的余弦函数值

6) math.tan(x)    #表示x的正切函数值

7)math.asin(x)    #表示x的反正弦函数值

8) math.acos(x)    #表示x的反余弦函数值

9) math.atan(x)    #表示x的反正切函数值

10) math.atan2(y,x)    #表示y/x的反正切函数值

11) math.sinh(x)    #表示x的双曲正弦函数值

12) math.cosh(x)    #表示x的双曲余弦函数值

13) math.tanh(x)    #表示x的双曲正切函数值

14) math.asinh(x)    #表示x的反双曲正弦函数值

15) math.acosh(x)    #表示x的反双曲余弦函数值

16) math.atanh(x)    #表示x的反双曲正切函数值

1)math.erf(x)    #高斯误差函数

2) math.erfc(x)    #余补高斯误差函数

3) math.gamma(x)    #伽马函数(欧拉第二积分函数)

4) math.lgamma(x)    #伽马函数的自然对数

怎样用python构建一个卷积神经网络?

用keras框架较为方便

首先安装anaconda,然后通过pip安装keras

1、#导入各种用到的模块组件

from __future__ import absolute_import

from __future__ import print_function

from keras.preprocessing.image import ImageDataGenerator

from keras.models import Sequential

from keras.layers.core import Dense, Dropout, Activation, Flatten

from keras.layers.advanced_activations import PReLU

from keras.layers.convolutional import Convolution2D, MaxPooling2D

from keras.optimizers import SGD, Adadelta, Adagrad

from keras.utils import np_utils, generic_utils

from six.moves import range

from data import load_data

import random

import numpy as np

np.random.seed(1024)  # for reproducibility

2、。#打乱数据

index = [i for i in range(len(data))]

random.shuffle(index)

data = data[index]

label = label[index]

print(data.shape[0], ' samples')

#label为0~9共10个类别,keras要求格式为binary class matrices,转化一下,直接调用keras提供的这个函数

label = np_utils.to_categorical(label, 10)

###############

#开始建立CNN模型

###############

#生成一个model

model = Sequential()

3、#第一个卷积层,4个卷积核,每个卷积核大小5*5。1表示输入的图片的通道,灰度图为1通道。

#border_mode可以是valid或者full,具体看这里说明:

#激活函数用tanh

#你还可以在model.add(Activation('tanh'))后加上dropout的技巧: model.add(Dropout(0.5))

model.add(Convolution2D(4, 5, 5, border_mode='valid',input_shape=(1,28,28)))

model.add(Activation('tanh'))

#第二个卷积层,8个卷积核,每个卷积核大小3*3。4表示输入的特征图个数,等于上一层的卷积核个数

4、全连接层,先将前一层输出的二维特征图flatten为一维的。

#Dense就是隐藏层。16就是上一层输出的特征图个数。4是根据每个卷积层计算出来的:(28-5+1)得到24,(24-3+1)/2得到11,(11-3+1)/2得到4

#全连接有128个神经元节点,初始化方式为normal

model.add(Flatten())

model.add(Dense(128, init='normal'))

model.add(Activation('tanh'))

#Softmax分类,输出是10类别

model.add(Dense(10, init='normal'))

model.add(Activation('softmax'))

#############

#开始训练模型

##############

#使用SGD + momentum

#model.compile里的参数loss就是损失函数(目标函数)

sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=True)

model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=["accuracy"])

#调用fit方法,就是一个训练过程. 训练的epoch数设为10,batch_size为100.

#数据经过随机打乱shuffle=True。verbose=1,训练过程中输出的信息,0、1、2三种方式都可以,无关紧要。show_accuracy=True,训练时每一个epoch都输出accuracy。

#validation_split=0.2,将20%的数据作为验证集。

model.fit(data, label, batch_size=100, nb_epoch=10,shuffle=True,verbose=1,validation_split=0.2)

"""

#使用data augmentation的方法

#一些参数和调用的方法,请看文档

datagen = ImageDataGenerator(

featurewise_center=True, # set input mean to 0 over the dataset

samplewise_center=False, # set each sample mean to 0

featurewise_std_normalization=True, # divide inputs by std of the dataset

samplewise_std_normalization=False, # divide each input by its std

zca_whitening=False, # apply ZCA whitening

rotation_range=20, # randomly rotate images in the range (degrees, 0 to 180)

width_shift_range=0.2, # randomly shift images horizontally (fraction of total width)

height_shift_range=0.2, # randomly shift images vertically (fraction of total height)

horizontal_flip=True, # randomly flip images

vertical_flip=False) # randomly flip images

# compute quantities required for featurewise normalization

# (std, mean, and principal components if ZCA whitening is applied)

datagen.fit(data)

for e in range(nb_epoch):

print('-'*40)

print('Epoch', e)

print('-'*40)

print("Training...")

# batch train with realtime data augmentation

progbar = generic_utils.Progbar(data.shape[0])

for X_batch, Y_batch in datagen.flow(data, label):

loss,accuracy = model.train(X_batch, Y_batch,accuracy=True)

progbar.add(X_batch.shape[0], values=[("train loss", loss),("accuracy:", accuracy)] )


当前标题:tanh函数python tanh函数的反函数
当前链接:http://scyanting.com/article/dogpcoj.html