Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. import h5py
  2. import numpy as np
  3. import tensorflow as tf
  4. import matplotlib.pyplot as plt
  5. import math as math
  6.  
  7. f = h5py.File('/Users/Krzysztof/PycharmProjects/untitled/venv/4studs.hdf5', 'r')
  8.  
  9. # f1 = f.get('eeg_subject_am1_trial_0_attended_female')
  10. # f2 = f.get('envelope_subject_am1_trial_0_sex_female')
  11. # f3 = f.get('eeg_subject_am1_trial_0_attended_male')
  12. # f4 = f.get('envelope_subject_am1_trial_0_sex_male')
  13. #
  14. # f5 = f.get('eeg_subject_am1_trial_1_attended_female')
  15. # f6 = f.get('envelope_subject_am1_trial_1_sex_female')
  16. # f7 = f.get('envelope_subject_am1_trial_1_sex_male')
  17. # eeg3 = np.array(f5)
  18. # env3 = np.array(f6)
  19. # env4 = np.array(f7)
  20. #
  21. # eeg2 = np.array(f3)
  22. # env2 = np.array(f4)
  23. #
  24. # eeg1 = np.array(f1)
  25. # env1 = np.array(f2)
  26. # eeg1_w1 = tf.slice(eeg1, [1, 0], [1, 6250])
  27. #
  28. # array1 = np.zeros(((6211, 40)))
  29. # for i in range(6211):
  30. # array1[i] = tf.slice(eeg1_w1, [0, i], [1, 40])
  31. #
  32. # array2 = np.zeros((6211,40))
  33. # for i in range(6211):
  34. # array2[i] = tf.slice(env1, [i], [40])
  35. # ###########
  36. #
  37. # ##TRIAL0
  38. # eeg_tab = np.zeros((32,6250))
  39. # for i in range(32):
  40. # eeg_tab[i] = tf.slice(eeg1, [i, 0], [1,6250])
  41. # eeg = tf.keras.utils.normalize(eeg_tab)
  42. #
  43. # eeg_split = np.zeros((32,6211,40))
  44. # for a in range(32):
  45. # for i in range(6211):
  46. # eeg_split[a,i] = tf.slice(eeg[a], [i], [40])
  47. #
  48. # ##TRIAL1
  49. # eeg_tab1 = np.zeros((32,6250))
  50. # for i in range(32):
  51. # eeg_tab1[i] = tf.slice(eeg2, [i, 0], [1,6250])
  52. # eeg2_2 = tf.keras.utils.normalize(eeg_tab1)
  53. #
  54. # eeg_split2 = np.zeros((32,6211,40))
  55. # for a in range(32):
  56. # for i in range(6211):
  57. # eeg_split2[a,i] = tf.slice(eeg2_2[a], [i], [40])
  58. # out2 = tf.slice(env2, [0], [6211])
  59. # new_out2 = np.zeros(((32,6211)))
  60. # for i in range(32):
  61. # new_out2[i] = out2
  62. #
  63. # ##TRIAL3
  64. # eeg_tab3 = np.zeros((32,6250))
  65. # for i in range(32):
  66. # eeg_tab3[i] = tf.slice(eeg3, [i, 0], [1,6250])
  67. # eeg3_3 = tf.keras.utils.normalize(eeg_tab3)
  68. #
  69. # eeg_split3 = np.zeros((32,6211,40))
  70. # for a in range(32):
  71. # for i in range(6211):
  72. # eeg_split3[a,i] = tf.slice(eeg3_3[a], [i], [40])
  73. # out3 = tf.slice(env3, [0], [6211])
  74. # new_out3 = np.zeros(((32,6211)))
  75. # for i in range(32):
  76. # new_out3[i] = out3
  77. # out4 = tf.slice(env4, [0], [6211])
  78. # new_out4 = np.zeros(((32,6211)))
  79. # for i in range(32):
  80. # new_out4[i] = out4
  81. # ###############
  82. # eeg_w2 = tf.slice(eeg1, [2, 0], [1, 6250])
  83. # eeg_w3 = tf.slice(eeg1, [3, 0], [1, 6250])
  84. # array3 = np.zeros(((6211, 40)))
  85. # array4 = np.zeros((6211,40))
  86. #
  87. # for i in range(6211):
  88. # array3[i] = tf.slice(eeg_w2, [0, i], [1, 40])
  89. # array4[i] = tf.slice(eeg_w3, [0, i], [1, 40])
  90. #
  91. # ex_1 = tf.keras.utils.normalize(array1)
  92. # ex_2 = tf.keras.utils.normalize(array3)
  93. # ex_3 = tf.keras.utils.normalize(array4)
  94. # out1 = tf.slice(env1, [0], [6211])
  95. # new_out = np.zeros(((32,6211)))
  96. # for i in range(32):
  97. # new_out[i] = out1
  98. #
  99. # ###########
  100. # output_1 = np.zeros((6211,1))
  101. # for i in range(6211):
  102. # output_1[i] = tf.slice(env1, [i], [1])
  103. #
  104. # #dataset_train = tf.data.Dataset.from_tensor_slices((array1,output_1))
  105. # #dataset_val = tf.data.Dataset.from_tensor_slices((ex_2,new_out))
  106. #
  107. # model = tf.keras.Sequential([
  108. # tf.keras.layers.Dense(64, activation='elu'),
  109. # tf.keras.layers.Dense(32, activation='elu'),
  110. # tf.keras.layers.Dense(1)
  111. # ])
  112. #
  113. # model.compile(optimizer='sgd',loss='mean_squared_error')
  114. #
  115. #
  116. # model.fit(x=eeg_split,y=new_out,epochs=2)
  117. # model.fit(x=eeg_split2,y=new_out2,epochs=2)
  118. #
  119. # prediction = model.predict(eeg_split3[0])
  120.  
  121.  
  122. #############################################
  123. # znorm = tf.keras.utils.normalize(f1)
  124. # t1 = tf.reshape(znorm, [1, 34, 6250, 1])
  125. # k = tf.constant(1, shape=(34,40), dtype = 'float64')
  126. # k1 = tf.reshape(k, [34, 40, 1, 1])
  127. # k2 = tf.reshape(tf.constant(1, shape=(1,40), dtype='float64'), [1, 40, 1, 1])
  128. # conv = tf.nn.conv2d(t1,k2,strides = [1, 1, 1, 1], padding='VALID')
  129. # print('conv: ',conv.shape)
  130. # conv_2 = tf.slice(conv, [0, 0, 0, 0], [1,32, 6211,1])
  131. # print('conv2: ',conv_2.shape)
  132. # shape = conv_2.get_shape().as_list()
  133. # conv3 = tf.reshape(conv_2, [32, 6211,-1])
  134. # print('conv3: ',conv3.shape)
  135. # conv4 = tf.reshape(conv3, [32,6211])
  136. # print(conv4.shape)
  137.  
  138. kern = tf.reshape(tf.constant(1, shape=(1,40), dtype='float64'), [1, 40, 1, 1])
  139. #AM_TR0_FEMALE
  140. f1 = f.get('eeg_subject_am1_trial_0_attended_female')
  141. f1_norm = tf.keras.utils.normalize(f1)
  142. f1_final = tf.reshape(f1_norm, [1, 34, 6250, 1])
  143. f1_conv = tf.nn.conv2d(f1_final, kern, strides=[1,1,1,1], padding='VALID')
  144. f1_conv2 = tf.slice(f1_conv, [0, 0, 0, 0], [1,32, 6211,1])
  145. f1_conv3 = tf.reshape(f1_conv2, [32, 6211,-1])
  146. f1_in = tf.reshape(f1_conv3, [32,6211])
  147. f1_input = tf.transpose(f1_in)
  148.  
  149. f1_out1 = f.get('envelope_subject_am1_trial_0_sex_female')
  150. f1_out = tf.slice(f1_out1, [0],[6211])
  151.  
  152. #AM_TR0_MALE
  153. f2 = f.get('eeg_subject_am1_trial_0_attended_male')
  154. f2_norm = tf.keras.utils.normalize(f2)
  155. f2_final = tf.reshape(f2_norm, [1, 34, 6250, 1])
  156. f2_conv = tf.nn.conv2d(f2_final, kern, strides=[1,1,1,1], padding='VALID')
  157. f2_conv2 = tf.slice(f2_conv, [0, 0, 0, 0], [1,32, 6211,1])
  158. f2_conv3 = tf.reshape(f2_conv2, [32, 6211,-1])
  159. f2_in = tf.reshape(f2_conv3, [32,6211])
  160. f2_input = tf.transpose(f2_in)
  161.  
  162. f2_out1 = f.get('envelope_subject_am1_trial_0_sex_male')
  163. f2_out = tf.slice(f2_out1, [0],[6211])
  164.  
  165. #MODEL
  166. model = tf.keras.Sequential([
  167. tf.keras.layers.Dense(128, activation='elu'),
  168. tf.keras.layers.Dense(1, activation='elu')
  169. ])
  170. model.compile(optimizer='sgd',loss='mean_squared_error')
  171. model.fit(input,output,epochs=10)
  172. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement