#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 4 15:43:05 2020
@author: li_hy
"""
import tensorflow as tf
import numpy as np
from scipy.io import loadmat as load
from tensorflow.keras import layers,optimizers,Sequential
from tensorflow.keras import Model
from tensorflow.keras.layers import Conv1D, BatchNormalization, Activation, MaxPool1D, Dropout, Flatten, Dense
from matplotlib import pyplot as plt
from sklearn.preprocessing import StandardScaler
import os
class Mynet(Model):
def __init__(self):
super(Mynet, self).__init__()
self.c1 = Conv1D(filters=3, kernel_size=3, activation='sigmoid')
self.b1 = BatchNormalization()
self.a1 = Activation('sigmoid')
self.p1 = MaxPool1D(pool_size=3, strides=2)
self.flatten = Flatten()
self.f1 = Dense(2048, activation='sigmoid')
self.f2 = Dense(3, activation='softmax')
def call(self, x):
x = self.c1(x)
x = self.b1(x)
x = self.a1(x)
x = self.p1(x)
#x = self.d(x)
x = self.flatten(x)
x = self.f1(x)
y = self.f2(x)
return y
model = Mynet()
opt=tf.keras.optimizers.SGD(
learning_rate=0.001, momentum=0.0, nesterov=False, name='SGD'
)
model.compile(optimizer=opt, loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
scaler = StandardScaler()
#数据归一化
x_train = scaler.fit_transform(x_train.astype(np.float32).reshape(-1,1)).reshape(-1,5,16384)
x_test =scaler.fit_transform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1,5,16384)
#print(x_train[0])
history = model.fit(x_train, y_train, batch_size=8, epochs=20, validation_data=(x_test, y_test), validation_freq=1)
#model.summary()
y=model.predict(x_train)
print(y)
print('-----------------')
print(y_train)
输出的预测结果和输入数据完全没有关系,而且数值几乎一模一样