我结合CNN和LSTM使用以下代码:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import itertools
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import tensorflow as tf
import pyfftw
from scipy import signal
import xlrd
from tensorflow.python.tools import freeze_graph
from tensorflow.python.tools import optimize_for_inference_lib
import seaborn as sns
from sklearn.metrics import confusion_matrix
##matplotlib inline
plt.style.use('ggplot')
## define funtions
def read_data(file_path):
## column_names = ['user-id','activity','timestamp', 'x-axis', 'y-axis', 'z-axis']
column_names = ['activity','timestamp', 'Ax', 'Ay', 'Az', 'Lx', 'Ly', 'Lz', 'Gx', 'Gy', 'Gz', 'Mx', 'My', 'Mz'] ## 3 sensors
data = pd.read_csv(file_path,header = None, names = column_names)
return data
def feature_normalize(dataset):
mu = np.mean(dataset,axis = 0)
sigma = np.std(dataset,axis = 0)
return (dataset - mu)/sigma
def plot_axis(ax, x, y, title):
ax.plot(x, y)
ax.set_title(title)
ax.xaxis.set_visible(False)
ax.set_ylim([min(y) - np.std(y), max(y) + np.std(y)])
ax.set_xlim([min(x), max(x)])
ax.grid(True)
def plot_activity(activity,data):
fig, (ax0, ax1, ax2) = plt.subplots(nrows = 3, figsize = (15, 10), sharex = True)
plot_axis(ax0, data['timestamp'], data['Ax'], 'x-axis')
plot_axis(ax1, data['timestamp'], data['Ay'], 'y-axis')
plot_axis(ax2, data['timestamp'], data['Az'], 'z-axis')
plt.subplots_adjust(hspace=0.2)
fig.suptitle(activity)
plt.subplots_adjust(top=0.90)
plt.show()
def windows(data, size):
start = 0
while start < data.count():
yield start, start + size
start += (size / 2)
def segment_signal(data, window_size = None, num_channels=None): # edited
segments = np.empty((0,window_size,num_channels)) #change from 3 to 9 channels for AGM fusion #use variable num_channels=9
labels = np.empty((0))
for (n_start, n_end) in windows(data['timestamp'], window_size):
## x = data["x-axis"][start:end]
## y = data["y-axis"][start:end]
## z = data["z-axis"][start:end]
n_start = int(n_start)
n_end = int(n_end)
Ax = data["Ax"][n_start:n_end]
Ay = data["Ay"][n_start:n_end]
Az = data["Az"][n_start:n_end]
Lx = data["Lx"][n_start:n_end]
Ly = data["Ly"][n_start:n_end]
Lz = data["Lz"][n_start:n_end]
Gx = data["Gx"][n_start:n_end]
Gy = data["Gy"][n_start:n_end]
Gz = data["Gz"][n_start:n_end]
Mx = data["Mx"][n_start:n_end]
My = data["My"][n_start:n_end]
Mz = data["Mz"][n_start:n_end]
if(len(data['timestamp'][n_start:n_end]) == window_size): # include only windows with size of 90
segments = np.vstack([segments,np.dstack([Ax,Ay,Az,Gx,Gy,Gz,Mx,My,Mz])])
labels = np.append(labels,stats.mode(data["activity"][n_start:n_end])[0][0])
return segments, labels
def weight_variable(shape, restore_name):
initial = tf.truncated_normal(shape, stddev = 0.1)
return tf.Variable(initial, name=restore_name)
def bias_variable(shape, restore_name):
initial = tf.constant(0.0, shape = shape)
return tf.Variable(initial, name=restore_name)
def depthwise_conv2d(x, W):
return tf.nn.depthwise_conv2d(x,W, [1, 1, 1, 1], padding='VALID')
def apply_depthwise_conv(x,weights,biases):
return tf.nn.relu(tf.add(depthwise_conv2d(x, weights),biases))
def apply_max_pool(x,kernel_size,stride_size):
return tf.nn.max_pool(x, ksize=[1, 1, kernel_size, 1],
strides=[1, 1, stride_size, 1], padding='VALID')
#------------------------get dataset----------------------#
## run shoaib_dataset.py to generate dataset_shoaib_total.txt
## get data from dataset_shoaib_total.txt
dataset_belt = read_data('dataset_shoaibsensoractivity_participant_belt.txt')
dataset_left_pocket = read_data('dataset_shoaibsensoractivity_participant_left_pocket.txt')
dataset_right_pocket = read_data('dataset_shoaibsensoractivity_participant_right_pocket.txt')
dataset_upper_arm = read_data('dataset_shoaibsensoractivity_participant_upper_arm.txt')
dataset_wrist = read_data('dataset_shoaibsensoractivity_participant_wrist.txt')
#--------------------preprocessing------------------------#
dataset_belt['Ax'] = feature_normalize(dataset_belt['Ax'])
dataset_belt['Ay'] = feature_normalize(dataset_belt['Ay'])
dataset_belt['Az'] = feature_normalize(dataset_belt['Az'])
dataset_belt['Gx'] = feature_normalize(dataset_belt['Gx'])
dataset_belt['Gy'] = feature_normalize(dataset_belt['Gy'])
dataset_belt['Gz'] = feature_normalize(dataset_belt['Gz'])
dataset_belt['Mx'] = feature_normalize(dataset_belt['Mx'])
dataset_belt['My'] = feature_normalize(dataset_belt['My'])
dataset_belt['Mz'] = feature_normalize(dataset_belt['Mz'])
dataset_left_pocket['Ax'] = feature_normalize(dataset_left_pocket['Ax'])
dataset_left_pocket['Ay'] = feature_normalize(dataset_left_pocket['Ay'])
dataset_left_pocket['Az'] = feature_normalize(dataset_left_pocket['Az'])
dataset_left_pocket['Gx'] = feature_normalize(dataset_left_pocket['Gx'])
dataset_left_pocket['Gy'] = feature_normalize(dataset_left_pocket['Gy'])
dataset_left_pocket['Gz'] = feature_normalize(dataset_left_pocket['Gz'])
dataset_left_pocket['Mx'] = feature_normalize(dataset_left_pocket['Mx'])
dataset_left_pocket['My'] = feature_normalize(dataset_left_pocket['My'])
dataset_left_pocket['Mz'] = feature_normalize(dataset_left_pocket['Mz'])
dataset_right_pocket['Ax'] = feature_normalize(dataset_right_pocket['Ax'])
dataset_right_pocket['Ay'] = feature_normalize(dataset_right_pocket['Ay'])
dataset_right_pocket['Az'] = feature_normalize(dataset_right_pocket['Az'])
dataset_right_pocket['Gx'] = feature_normalize(dataset_right_pocket['Gx'])
dataset_right_pocket['Gy'] = feature_normalize(dataset_right_pocket['Gy'])
dataset_right_pocket['Gz'] = feature_normalize(dataset_right_pocket['Gz'])
dataset_right_pocket['Mx'] = feature_normalize(dataset_right_pocket['Mx'])
dataset_right_pocket['My'] = feature_normalize(dataset_right_pocket['My'])
dataset_right_pocket['Mz'] = feature_normalize(dataset_right_pocket['Mz'])
dataset_upper_arm['Ax'] = feature_normalize(dataset_upper_arm['Ax'])
dataset_upper_arm['Ay'] = feature_normalize(dataset_upper_arm['Ay'])
dataset_upper_arm['Az'] = feature_normalize(dataset_upper_arm['Az'])
dataset_upper_arm['Gx'] = feature_normalize(dataset_upper_arm['Gx'])
dataset_upper_arm['Gy'] = feature_normalize(dataset_upper_arm['Gy'])
dataset_upper_arm['Gz'] = feature_normalize(dataset_upper_arm['Gz'])
dataset_upper_arm['Mx'] = feature_normalize(dataset_upper_arm['Mx'])
dataset_upper_arm['My'] = feature_normalize(dataset_upper_arm['My'])
dataset_upper_arm['Mz'] = feature_normalize(dataset_upper_arm['Mz'])
dataset_wrist['Ax'] = feature_normalize(dataset_wrist['Ax'])
dataset_wrist['Ay'] = feature_normalize(dataset_wrist['Ay'])
dataset_wrist['Az'] = feature_normalize(dataset_wrist['Az'])
dataset_wrist['Gx'] = feature_normalize(dataset_wrist['Gx'])
dataset_wrist['Gy'] = feature_normalize(dataset_wrist['Gy'])
dataset_wrist['Gz'] = feature_normalize(dataset_wrist['Gz'])
dataset_wrist['Mx'] = feature_normalize(dataset_wrist['Mx'])
dataset_wrist['My'] = feature_normalize(dataset_wrist['My'])
dataset_wrist['Mz'] = feature_normalize(dataset_wrist['Mz'])
#------------------fixed hyperparameters--------------------#
window_size = 200 #from 90 #FIXED at 4 seconds
#----------------input hyperparameters------------------#
input_height = 1
input_width = window_size
num_labels = 7
num_channels = 9 #from 3 channels #9 channels for AGM
#-------------------sliding time window----------------#
segments_belt, labels_belt = segment_signal(dataset_belt, window_size=window_size, num_channels=num_channels)
labels_belt = np.asarray(pd.get_dummies(labels_belt), dtype = np.int8)
reshaped_segments_belt = segments_belt.reshape(len(segments_belt), (window_size*num_channels)) #use variable num_channels instead of constant 3 channels
segments_left_pocket, labels_left_pocket = segment_signal(dataset_left_pocket, window_size=window_size, num_channels=num_channels)
labels_left_pocket = np.asarray(pd.get_dummies(labels_left_pocket), dtype = np.int8)
reshaped_segments_left_pocket = segments_left_pocket.reshape(len(segments_left_pocket), (window_size*num_channels)) #use variable num_channels instead of constant 3 channels
segments_right_pocket, labels_right_pocket = segment_signal(dataset_right_pocket, window_size=window_size, num_channels=num_channels)
labels_right_pocket = np.asarray(pd.get_dummies(labels_right_pocket), dtype = np.int8)
reshaped_segments_right_pocket = segments_right_pocket.reshape(len(segments_right_pocket), (window_size*num_channels)) #use variable num_channels instead of constant 3 channels
segments_upper_arm, labels_upper_arm = segment_signal(dataset_upper_arm, window_size=window_size, num_channels=num_channels)
labels_upper_arm = np.asarray(pd.get_dummies(labels_upper_arm), dtype = np.int8)
reshaped_segments_upper_arm = segments_upper_arm.reshape(len(segments_upper_arm), (window_size*num_channels)) #use variable num_channels instead of constant 3 channels
segments_wrist, labels_wrist = segment_signal(dataset_wrist, window_size=window_size, num_channels=num_channels)
labels_wrist = np.asarray(pd.get_dummies(labels_wrist), dtype = np.int8)
reshaped_segments_wrist = segments_wrist.reshape(len(segments_wrist), (window_size*num_channels)) #use variable num_channels instead of constant 3 channels
##reshaped_segments = np.vstack([reshaped_segments1,reshaped_segments2,reshaped_segments3,reshaped_segments4,reshaped_segments5,reshaped_segments6,reshaped_segments7,reshaped_segments8,reshaped_segments9,reshaped_segments10])
##labels = np.vstack([labels1,labels2,labels3,labels4,labels5,labels6,labels7,labels8,labels9,labels10])
# all locations
reshaped_segments = np.vstack([reshaped_segments_belt,reshaped_segments_left_pocket,reshaped_segments_right_pocket,reshaped_segments_upper_arm,reshaped_segments_wrist])
labels = np.vstack([labels_belt,labels_left_pocket,labels_right_pocket,labels_upper_arm,labels_wrist])
#------------divide data into test and training `set-----------#
train_test_split = np.random.rand(len(reshaped_segments)) < 0.70
train_x = reshaped_segments[train_test_split]
train_y = labels[train_test_split]
test_x = reshaped_segments[~train_test_split]
test_y = labels[~train_test_split]
#---------------training hyperparameters----------------#
batch_size = 10
kernel_size = 60 #from 60 #optimal 2
depth = 15 #from 60 #optimal 15
num_hidden = 1000 #from 1000 #optimal 80
learning_rate = 0.0001
training_epochs = 8
total_batches = train_x.shape[0] ##// batch_size # included // batch_size
#---------define placeholders for input----------#
X = tf.placeholder(tf.float32, shape=[None,input_width * num_channels], name="input")
X_reshaped = tf.reshape(X,[-1,input_height,input_width,num_channels])
Y = tf.placeholder(tf.float32, shape=[None,num_labels])
#---------------------perform convolution-----------------#
# first convolutional layer
c_weights = weight_variable([1, kernel_size, num_channels, depth], restore_name="c_weights")
c_biases = bias_variable([depth * num_channels], restore_name="c_biases")
c = apply_depthwise_conv(X_reshaped,c_weights,c_biases)
p = apply_max_pool(c,20,2)
# second convolutional layer
c2_weights = weight_variable([1, 6,depth*num_channels,depth//10], restore_name="c2_weights")
c2_biases = bias_variable([(depth*num_channels)*(depth//10)], restore_name="c2_biases")
c2 = apply_depthwise_conv(p,c2_weights,c2_biases)
n_classes = 7
n_hidden = 128
n_inputs = 540 # 540 = 60*3 not 180 # or 7*9*10
lstm_size = 128
rnnW = {
'hidden': tf.Variable(tf.random_normal([n_inputs, n_hidden])),
'output': tf.Variable(tf.random_normal([n_hidden, n_classes]))
}
rnnBiases = {
'hidden': tf.Variable(tf.random_normal([n_hidden], mean=1.0)),
'output': tf.Variable(tf.random_normal([n_classes]))
}
c2Reshape = tf.reshape(c2, [-1, 7, 200])
shuff = tf.transpose(c2Reshape, [1, 0, 2])
shuff = tf.reshape(shuff, [-1, n_inputs])
# Linear activation, reshaping inputs to the LSTM's number of hidden:
hidden = tf.nn.relu(tf.matmul(
shuff, rnnW['hidden']
) + rnnBiases['hidden'])
# Split the series because the rnn cell needs time_steps features, each of shape:
hidden = tf.split(axis=0, num_or_size_splits=7, value=hidden)
lstm_cell = tf.contrib.rnn.LSTMCell(lstm_size, forget_bias=1.0)
# Stack two LSTM layers, both layers has the same shape
lstm_layers = tf.contrib.rnn.MultiRNNCell([lstm_cell] * 2)
lstmOutputs, _ = tf.contrib.rnn.static_rnn(lstm_layers, hidden, dtype=tf.float32)
lstmLastOutput = lstmOutputs[-1]
y_ = tf.matmul(lstmLastOutput, rnnW['output']) + rnnBiases['output']
#-----------------loss optimization-------------#
loss = -tf.reduce_sum(Y * tf.log(y_))
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(loss)
##optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(loss)
#-----------------compute accuracy---------------#
correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
cost_history = np.empty(shape=[1],dtype=float)
saver = tf.train.Saver()
#-----------------run session--------------------#
session = tf.Session()
session.run(tf.global_variables_initializer())
for epoch in range(8):
for b in range(total_batches):
offset = (b * batch_size) % (train_y.shape[0] - batch_size)
batch_x = train_x[offset:(offset + batch_size), :]
batch_y = train_y[offset:(offset + batch_size), :]
_, c = session.run([optimizer, loss],feed_dict={X: batch_x, Y : batch_y})
cost_history = np.append(cost_history,c)
print("Epoch: ",epoch," Training Loss: ",c," Training Accuracy: ",\
session.run(accuracy, feed_dict={X: train_x, Y: train_y}))
print("Testing Accuracy:", session.run(accuracy, feed_dict={X: test_x, Y: test_y}))
if 1==1:
print ("Testing Accuracy: ", session.run(accuracy, feed_dict={X: test_x, Y: test_y}),'\n')
pred_y = session.run(tf.argmax(y_ ,1),feed_dict={X: test_x})
cm = confusion_matrix(np.argmax(test_y ,1),pred_y)
print (cm, '\n')
plt.imshow(cm)
plt.title('Confusion Matrix')
plt.rcParams['image.cmap'] = 'afmhot'
plt.colorbar()
tick_marks = np.arange(len(['Wal', 'Std', 'Jog', 'Sit', 'Bik', 'Wlu', 'Wld']))
plt.xticks(tick_marks, ['Wal', 'Std', 'Jog', 'Sit', 'Bik', 'Wlu', 'Wld'])
plt.yticks(tick_marks, ['Wal', 'Std', 'Jog', 'Sit', 'Bik', 'Wlu', 'Wld'])
fmt = '.2f'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.figure()
plt.show()
但是,我总是会遇到以下错误:
回溯(最后一次调用):文件“C:\Users\Charlene\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\client\session.py”,第1322行,在“do\u call return fn(*args)文件“C:\Users\Charlene\AppData\Local\Programs\Python\Python\Python35\lib\site packages\tensorflow\client\session.Python”中,第1307行,在“run\fn选项”中,feed\u dict、fetch\u list、target\u list、run\u metadata)文件“C:\Users\Charlene\AppData\Local\Programs\Python35\lib\site packages\tensorflow\Python\client\session.py”,第1409行,位于调用\u tf\u sessionrun\u metadata)tensorflow中。python框架错误。InvalidArgumentError:不兼容的形状:[10,7]vs[20,7][[Node:mul=mul[T=DT\u FLOAT,\u device=“/job:localhost/replica:0/task:0/device:CPU:0”](\u arg\u占位符\u 0\u 0,Log)]]
在处理上述异常期间,发生了另一个异常:
回溯(最近的最后一次调用):文件“”,第6行,在uC=会话中。运行([optimizer,loss],feed_dict={X:batch_X,Y:batch_Y})文件“C:\Users\Charlene\AppData\Local\Programs\Python35\lib\site packages\tensorflow\Python\client\session.py”,第900行,在运行元数据(run-run-run-metadata)文件“C:\Users\Charlene\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\client\session.py”中,第1135行,在第1316行“C:\Users\Charlene\AppData\Local\Programs\Python35\lib\site packages\tensorflow\Python\client\session.py”文件“C:\Users\Charlene\AppData\Local\Programs\Python35\lib\site packages\tensorflow\Python\client\session.py”文件“C:\Users\Charlene\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\client\session.py”第13,在_do_call raise type(e)(节点定义、操作、消息)tensorflow中。python框架错误。InvalidArgumentError:不兼容的形状:[10,7]vs[20,7][[Node:mul=mul[T=DT\u FLOAT,\u device=“/job:localhost/replica:0/task:0/device:CPU:0”](\u arg\u占位符\u 0\u 0,Log)]]
由op“mul”引起,定义于:文件“”,第1行,文件“C:\Users\Charlene\AppData\Local\Programs\Python\Python35\lib\idlelib\run.py”,第130行,主ret=method(*args,**kwargs)文件“C:\Users\Charlene\AppData\Local\Programs\Python\Python35\lib\idlelib\run.py”,第357行,运行代码执行(代码,self.locals)文件“,第2行,文件中“C:\Users\Charlene\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\ops\math\u ops。py”,第979行,在二进制\u op\u包装返回func(x,y,name=name)文件“C:\Users\Charlene\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\ops\math\u ops”中。py“,第1211行,在发送返回gen\u math\u ops.mul(x,y,name=name)文件“C:\Users\Charlene\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\ops\gen\u math\u ops”中。py”,第5066行,在mul“mul”中,x=x,y=y,name=name)文件“C:\Users\Charlene\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\framework\op\u def\u library”。py”,第787行,在文件“C:\Users\Charlene\AppData\Local\Programs\Python35\lib\site packages\tensorflow\Python\framework\ops”中。py”,第3392行,在create_op_def=op_def)文件“C:\Users\Charlene\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\framework\ops”中。py“,第1718行,在init self._traceback=self._graph._extract_stack()35; pylint:disable=protected access中
InvalidArgumentError(回溯见上文):不兼容的形状:[10,7]与[20,7][[Node:mul=mul[T=DT\u FLOAT,\u device=“/job:localhost/replica:0/task:0/device:CPU:0”](\u arg\u占位符\u 0\u 0,Log)]]
我看到的主要错误是:
不兼容的形状:[10,7]与[20,7]
其中10是批量大小,7是类数。
什么是错误?
错误似乎发生在这里:
loss = -tf.reduce_sum(Y * tf.log(y_))
您的Y
是(10,7)
,这是预期的,但由于某种原因Y
是(20,7)
。
尝试在这行之前跟踪c2
的形状:
c2Reshape = tf.reshape(c2, [-1, 7, 200])
和它后面的c2Reshape
的形状(或者只是暂时用c2Reshape=tf.reshape(c2,[10,7,200])
替换那一行,看看是否失败),我怀疑这就是20来自。
本文向大家介绍Java中char[] 和 String 类型占用字节大小问题,包括了Java中char[] 和 String 类型占用字节大小问题的使用技巧和注意事项,需要的朋友参考一下 作者:威威喵 原文链接:https://blog.csdn.net/smile_Running/article/details/87211916 在 C 语言中 1、char a[10] = {"China"}
问题内容: 对于我的项目,我需要将有向图转换为图的张量流实现,就好像它是神经网络一样。在tensorflow版本1中,我可以将所有输入定义为占位符,然后使用广度优先搜索图为输出生成数据流图。然后,我只需使用feed_dict来输入我的输入。但是,在TensorFlow v2.0中,他们决定完全放弃占位符。 如何在不使用占位符的情况下为每个接受可变数量的输入并返回可变数量的输出的图制作tf.func
在本章中,将重点介绍CNN和RNN之间的区别,它们的区别如下表中所示 - CNN RNN 它适用于图像等空间数据。 RNN适用于时间数据,也称为顺序数据。 CNN比RNN更强大。 与CNN相比,RNN包含更少的功能兼容性。 CNN采用固定大小的输入并生成固定大小的输出。 RNN可以处理任意长度大小输入/输出。 CNN是一种前馈人工神经网络,具有多层感知器的变化,旨在使用最少量的预处理。 与前馈神经
2 个轮次后达到 0.8498 的测试精度。K520 GPU 上为 41 秒/轮次。 from __future__ import print_function from keras.preprocessing import sequence from keras.models import Sequential from keras.layers import Dense, Dropout,
问题内容: 我有一个Angular JS v1.2.5表单,该表单在IE11中不起作用。它可以在Firefox,Chrome,Safari中正常运行。我的表单使用占位符属性内有插值的文本区域。 如果使用插值指定占位符属性,则会出现以下错误(仅在IE中)。 这是一个可在Firefox,Chrome,Safari和IE11中正常运行的Plnkr。 http://plnkr.co/edit/4cJzxt
g似乎接受和的任何组合作为初始和后续返回类型: 然而,clang拒绝了和,他说:错误:带有尾部返回类型的函数必须指定返回类型“auto”,而不是“decltype(auto)”(演示)。 哪个编译器是正确的?在每种情况下应该使用哪条规则(或)?在尾部返回类型中使用占位符类型有意义吗?