神经网络的输出值总是一样

  统计/机器学习 深度学习 损失函数 人工神经网络 TensorFlow    浏览次数:2692        分享
0


#!/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)


输出的预测结果和输入数据完全没有关系,而且数值几乎一模一样

 

lixixi   2020-05-06 18:35



   1个回答 
0

你没有train model 啊。Keras 应该是model.fit()

SofaSofa数据科学社区DS面试题库 DS面经

anlijuncn   2020-08-04 20:06



  相关讨论

关于利用卷积神经网络多任务学习的loss问题

关于卷积神经网络通道内容的问题

tensorflow如何实现F1值作为损失函数?

Tensorflow多层神经网络训练最后输出的值都一样的,找不到是什么原因?

训练时的数据集问题

tensorflow里面怎么自定义一个loss function?

tensorflow一定要用gpu吗?

关于tensorflow2的一点问题

BatchNormalization

tensorflow 训练的时候输出nan

  随便看看

seaborn如何显示图?

为啥Xgboost比GradientBoost好那么多?

为什么图的拉普拉斯矩阵的最小特征值一定是0?

python(matplotlib)中如何把折线图和柱状图画在一起?

推荐系统算法里的cold start是什么意思?