简单的cnn非线性回归

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#使用numpy生成200个随机点,np.newaxis 增加一个维度
x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis]
noise = np.random.normal(0,0.02,x_data.shape)

y_data = np.square(x_data) + noise
#定义两个placeholder,任意行,1列
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
#定义神经网络中间层
#tf.random_normal(([n,m]))是生成一个服从正太分布的n行m列的矩阵
weight_L1 = tf.Variable(tf.random_normal([1,10]))
biases_L1 = tf.Variable(tf.zeros([1,10]))
Wx_plus_b_L1 = tf.matmul(x,weight_L1) +biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)
#3定义神经网络输出层
weight_L2 = tf.Variable(tf.random_normal([10,1]))
biases_L2 = tf.Variable(tf.zeros([1,1]))
Wx_plus_b_L2 = tf.matmul(L1,weight_L2)+biases_L2
prediction = tf.nn.tanh(Wx_plus_b_L2)


#二次代价函数
#求方差平均值
loss = tf.reduce_mean(tf.square(y-prediction))
#使用梯度下降法,学习率为0.1
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

with tf.Session() as sess:
    #变量初始化
    sess.run(tf.global_variables_initializer())
    for i in range(2000):
        sess.run(train_step,feed_dict={x:x_data,y:y_data})

    #获得预测值
    prediction_value = sess.run(prediction,feed_dict={x:x_data})
    #画图
    plt.figure()
    plt.scatter(x_data,y_data)
    plt.plot(x_data,prediction_value,'r-')
    plt.show()
 
#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

tf.equal(A, B)是对比这两个矩阵或者向量的相等的元素,如果是相等的那就返回True,反正返回False,返回的值的矩阵维度和A是一样的import tensorflow as tf
import numpy as np
 
A = [[1,3,4,5,6]]
B = [[1,3,4,3,2]]
 
with tf.Session() as sess:
    print(sess.run(tf.equal(A, B)))

 

输出:

[[ True  True  True False False]]

tf.argmax(vector, 1):返回的是vector中的最大值的索引号,如果vector是一个向量,那就返回一个值,如果是一个矩阵,那就返回一个向量,这个向量的每一个维度都是相对应矩阵行的最大值元素的索引号。

import tensorflow as tf
import numpy as np
 
A = [[1,3,4,5,6]]
B = [[1,3,4], [2,4,1]]
 
with tf.Session() as sess:
    print(sess.run(tf.argmax(A, 1)))
    print(sess.run(tf.argmax(B, 1)))

输出:

[4]
[2 1]

在广义线性模型中,Logistic回归模型用于进行二分类任务,但它也可以通过一些技巧进行非线性分类。下面是一些常用的方法: 1. 特征转换:通过对输入特征进行非线性转换,可以使得Logistic回归模型能够处理非线性分类问题。常用的转换方法包括多项式特征扩展、指数函数、对数函数等。可以通过将原始特征进行组合或者应用非线性函数来引入非线性。 2. 核技巧(Kernel Trick):通过在Logistic回归模型中使用核函数,可以将输入特征映射到高维空间中,从而实现非线性分类。常用的核函数有多项式核函数、高斯核函数等,它们能够将低维非线性可分的数据映射到高维线性可分的空间。 3. 集成学习方法:可以将多个Logistic回归模型进行集成,例如使用bagging、boosting等方法。通过组合多个基础模型的预测结果,可以得到更好的非线性分类效果。 4. 神经网络方法:神经网络模型本身就能够处理非线性分类问题。你可以尝试使用基于神经网络的分类模型,如多层感知机(MLP)或卷积神经网络(CNN),这些模型在处理非线性分类问题上具有较强的能力。 需要注意的是,以上方法都是在Logistic回归模型的基础上进行的改进,通过引入非线性元素来实现非线性分类。这些方法可以根据具体问题选择并结合使用,以获得更好的分类效果。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值