一:所用的神经网络——cnn
1;心音数据特点:是1维的
2:用到的卷积,池化层等也是1d 的(用法和2d的差不多,只是输入是3D的[batch,n_w,n_c],相对于2d少了高度n_h):如下:tf.nn.conv1d(),tf.layers.max_pooling1d()
input_tmp=input_data
###第一层卷积
conv1=tf.nn.conv1d(input_tmp,parameters["FW1"],stride=1,padding="VALID")
Z1=tf.nn.bias_add(conv1,parameters["b1"])
A1=tf.nn.relu(Z1)
P1= tf.layers.max_pooling1d(A1,pool_size=[2],strides=[2],padding="valid")
###第二层卷积
conv2=tf.nn.conv1d(P1,parameters["FW2"],stride=1,padding="VALID")
Z2=tf.nn.bias_add(conv2,parameters["b2"])
A2=tf.nn.relu(Z2)
P2=tf.layers.max_pooling1d(A2,pool_size=[2],strides=[2],padding="valid")
#flatten
P2=tf.contrib.layers.flatten(P2)
###fully_connected
fc1=tf.contrib.layers.fully_connected(P2,20)
output=tf.contrib.layers.fully_connected(fc1,1)
2:初始变量。(采用了单独定义)
def weight_initializer(weight_sizes=[[5,8],[5,4]]): ###语音 1维, 通道数也为 :1 -->>[1,5,1,8] 和[1,5,1,4]
parameters={}
length=len(weight_sizes)
for i,weight_size in enumerate(weight_sizes):
if i==0:
parameters["FW"+str((i+1)+10)]=tf.get_variable("FW"+str((i+1)+10),[weight_size[0],1,weight_size[1]],
initializer=tf.truncated_normal_initializer(stddev=0.1))
parameters["b"+str((i+1)+10)]=tf.get_variable("b"+str((i+1)+10),
[weight_size[1]],initializer=tf.constant_initializer(0.1))
else:
parameters["FW"+str((i+1)+10)]=tf.get_variable("FW"+str((i+1)+10),[weight_size[0],weight_sizes[i-1][1],weight_size[1]], initializer=tf.truncated_normal_initializer(stddev=0.1)) ##权重和上一个通道匹配
parameters["b"+str((i+1)+10)]=tf.get_variable("b"+str((i+1)+10),
[weight_size[1]],initializer=tf.constant_initializer(0.1))
return parameters
二:遇到的问题及解决办法:
1. Incompatible shapes: [160,1] vs. [2,1]
[[Node: sub = Sub[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](Reshape_2, Reshape_3)]] ### 拉直时,拉错了方向,导致维度匹配不上,重新tf.reshape()
2.ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`. ppp2.shape: (10, ?)
这种情况是由于 tf.reshape ()时,设定大小时存在张量,而不是确切的常数: 如:tf.reshape (x,[-1,y.shape[0]]) ,有时因 y.shape[0],导致识别不了,这种情况,只需中途找个变量(转化为 int )过渡一下(v =int(y.shape[0])),然后再 tf.reshape (x,[-1,v]) 就可以了
3.TypeError: unsupported operand type(s) for +: 'int' and 'Dimension'
如:tmp03=1+ppp[0].shape[1] 解决办法:将两者交换顺序即可:即 tmp03=ppp[0].shape[1] +1 (其他的运算操作也一样)