线性模型的神经网络

成像系统可以看作一个低通线性系统,由输出图像重构高分辨率图像是一个病态问题,可以说是纯靠猜测,如果有先验信息可以猜的准一些。神经网络通过复杂的网络模型逼近系统的真实特性,总结信息的先验知识,对这类问题具有比较好的效果。

                                                                                                                                               以上是我对神经网络的思考和认识

但真正使用起来仍会有很多麻烦,神经网络并不是万能的,是适用性的。如何根据具体问题构建相应的神经网络,不知道有没有类似的分析。目前在搭建一个网络用于标定成像系统,系统的模型是线性的(Y=MXN),最初是利用tensorflow搭建了一个神经元,用回归的方法训练,规模小的时候能够得到比较好的结果,但是X的规模大了就不是很好了。然后增加了几层,损失函数没有下降的趋势,目前卡住了。。。。


#encoding:utf-8



 

import tensorflow as tf
import numpy as np
import random
import matplotlib.image as mpimg
from scipy.linalg import toeplitz

BATCH_SIZE = 2  
SEED = 12565    # 产生统一的随机数
inputdir='xxx'

rdm = np.random.RandomState(SEED)

m=128
n=128
p=960
q=1280
batch_size=5
Y_=np.zeros((m,n,batch_size))
X=np.zeros((p,q,batch_size))


"""
随机生成的训练数据
X = rdm.rand(32, 32,100) #读取图像size=m,n
W = rdm.rand(1, 32)  #读取变换矩阵  size=p,m
W1=toeplitz(W)
W2=W1.T
m,n,bound=X.shape
p,m=W1.shape
noise = rdm.rand(32, 32,bound) #生成噪声


Y_=np.zeros((32,32,bound))
for s in range(0,bound,1):
    Y_[:,:,s] = np.matmul(np.matmul(W1,X[:,:,s]),W2)+noise[:,:,s] #生成真值Y=wx+noise size=p,n

"""
# 添加层
def add_layer1(inputs, out_size, activation_function=None):
   # add one more layer and return the output of this layer
   m=inputs.shape[0].value
   n=inputs.shape[1].value
   p=inputs.shape[2].value
   Weights = tf.Variable(tf.random_normal([ out_size,m])+1)
   biases = tf.Variable(tf.zeros([ out_size, n, p]) + 0.01)
   Wx_plus_b = tf.einsum('ij,jkl->ikl', Weights,inputs)
   if activation_function is None:
       outputs = Wx_plus_b
   else:
       outputs = activation_function(Wx_plus_b)
   return outputs

def add_layer2(inputs, out_size, activation_function=None):
   # add one more layer and return the output of this layer
   m=inputs.shape[0].value
   n=inputs.shape[1].value
   p=inputs.shape[2].value
   Weights = tf.Variable(tf.random_normal([n, out_size])+1)
   biases = tf.Variable(tf.zeros([m, out_size, p]) + 0.01)
   Wx_plus_b = tf.einsum('ijk,jl->ilk', inputs, Weights)+biases
   if activation_function is None:
       outputs = Wx_plus_b
   else:
       outputs = activation_function(Wx_plus_b)
   return outputs 
# 1定义神经网络的输入、参数和输出,定义前向传播过程。

y_ = tf.placeholder(tf.float32, shape=(m,n,batch_size))
x = tf.placeholder(tf.float32, shape=(p,q,batch_size))


#y0=add_layer1(x,512,activation_function=tf.nn.relu)
y1=add_layer1(x,256,activation_function=tf.nn.sigmoid)
y2=add_layer1(y1,128,activation_function=tf.nn.sigmoid)
#y3=add_layer2(y2,512,activation_function=tf.nn.relu)
#y4=add_layer2(y2,256,activation_function=tf.nn.relu)
y5=add_layer2(y2,128,activation_function=tf.nn.sigmoid)

#y0 = tf.matmul(w1, x)
#y = tf.matmul(y0,w2)
#y = tf.matmul(a, w2)

# 2定义损失函数及反向传播方法。
loss = tf.reduce_mean(tf.square(y5 - y_))
train_step = tf.train.GradientDescentOptimizer(1).minimize(loss) 
#train_step = tf.train.MomentumOptimizer(0.001,0.9).minimize(loss)
#train_step = tf.train.AdamOptimizer(0.1).minimize(loss)

# 3生成会话,训练STEPS轮
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    # 输出目前(未经训练)的参数取值。
    #print("w1:\n", sess.run(w1))
    #print("w2:\n", sess.run(w2))
    print("\n")
        
    # 训练模型。
    STEPS = 90000
    t=0
    for i in range(STEPS): 
                      #0-2999
#        if t<10:
        batch=random.sample(range(97),k=batch_size)
        for m in range(batch_size):
            file=(inputdir+'%d-1.tif' % (batch[m]+1))
            I=mpimg.imread(file)
            X[:,:,m]=np.double(I[:,:,2])/255-0.5
            file=(inputdir+'%d.tif' % (batch[m]+1))
            I=mpimg.imread(file)
            Y_[:,:,m]=np.double(I[:,:,2])
       
        sess.run(train_step, feed_dict={x: X, y_: Y_})
        t=t+1
        if t==50:
            total_loss = sess.run(loss,feed_dict={x: X, y_: Y_})
            print("After %d training step(s), loss on all data is %g"%(i,total_loss))
            t=0

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值