分享好友 人工智能首页 频道列表

Keras MAE和MSE source code

keras教程  2023-02-09 19:228010
def mean_squared_error(y_true, y_pred):
    if not K.is_tensor(y_pred):
        y_pred = K.constant(y_pred)
    y_true = K.cast(y_true, y_pred.dtype)
    return K.mean(K.square(y_pred - y_true), axis=-1)


def mean_absolute_error(y_true, y_pred):
    if not K.is_tensor(y_pred):
        y_pred = K.constant(y_pred)
    y_true = K.cast(y_true, y_pred.dtype)
    return K.mean(K.abs(y_pred - y_true), axis=-1)


def mean_absolute_percentage_error(y_true, y_pred):
    if not K.is_tensor(y_pred):
        y_pred = K.constant(y_pred)
    y_true = K.cast(y_true, y_pred.dtype)
    diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),
                                            K.epsilon(),
                                            None))
    return 100. * K.mean(diff, axis=-1)


def mean_squared_logarithmic_error(y_true, y_pred):
    if not K.is_tensor(y_pred):
        y_pred = K.constant(y_pred)
    y_true = K.cast(y_true, y_pred.dtype)
    first_log = K.log(K.clip(y_pred, K.epsilon(), None) + 1.)
    second_log = K.log(K.clip(y_true, K.epsilon(), None) + 1.)
return K.mean(K.square(first_log - second_log), axis=-1)

查看更多关于【keras教程】的文章

展开全文
相关推荐
反对 0
举报 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
Keras2.2 predict和fit_generator的区别
查看keras文档中,predict函数原型:predict(self, x, batch_size=32, verbose=0)说明:只使用batch_size=32,也就是说每次将batch_size=32的数据通过PCI总线传到GPU,然后进行预测。在一些问题中,batch_size=32明显是非常小的。而通过PCI传数据是非常耗时的

0评论2023-02-09861

keras模块学习之-激活函数(activations)--笔记
本笔记由博客园-圆柱模板 博主整理笔记发布,转载需注明,谢谢合作!   每一个神经网络层都需要一个激活函数,例如一下样例代码:           from keras.layers.core import Activation, Densemodel.add(Dense(64))model.add(Activation('tanh'))或把

0评论2023-02-09550

keras: 在构建LSTM模型时,使用变长序列的方法
众所周知,LSTM的一大优势就是其能够处理变长序列。而在使用keras搭建模型时,如果直接使用LSTM层作为网络输入的第一层,需要指定输入的大小。如果需要使用变长序列,那么,只需要在LSTM层前加一个Masking层,或者embedding层即可。from keras.layers import

0评论2023-02-09679

keras channels_last、preprocess_input、全连接层Dense、SGD优化器、模型及编译
channels_last 和 channels_firstkeras中 channels_last 和 channels_first 用来设定数据的维度顺序(image_data_format)。对2D数据来说,"channels_last"假定维度顺序为 (rows,cols,channels), 而"channels_first"假定维度顺序为(channels, rows, cols)。

0评论2023-02-091012

将keras的h5模型转换为tensorflow的pb模型 keras 调用h5模型
h5_to_pb.pyfrom keras.models import load_modelimport tensorflow as tfimport os import os.path as ospfrom keras import backend as K#路径参数input_path = 'input path'weight_file = 'weight.h5'weight_file_path = osp.join(input_path,weight_file

0评论2023-02-09773

Keras网络层之“关于Keras的层(Layer)” keras的embedding层
关于Keras的“层”(Layer)所有的Keras层对象都有如下方法:layer.get_weights():返回层的权重(numpy array)layer.set_weights(weights):从numpy array中将权重加载到该层中,要求numpy array的形状与layer.get_weights()的形状相同layer.get_config():返回

0评论2023-02-09888

Keras分类问题 keras 分类模型
#-*- coding: utf-8 -*-#使用神经网络算法预测销量高低import pandas as pd#参数初始化inputfile = 'data/sales_data.xls'data = pd.read_excel(inputfile, index_col = u'序号') #导入数据#数据是类别标签,要将它转换为数据#用1来表示“好”、“是”、“高

0评论2023-02-09923

tf.keras遇见的坑:Output tensors to a Model must be the output of a TensorFlow `Layer`
经过网上查找,找到了问题所在:在使用keras编程模式是,中间插入了tf.reshape()方法便遇到此问题。 解决办法:对于遇到相同问题的任何人,可以使用keras的Lambda层来包装张量流操作,这是我所做的:embed1 = keras.layers.Embedding(10000, 32)(inputs) # e

0评论2023-02-09963

更多推荐