我正在使用LSTM处理时间序列预测问题.输入包含多个功能,因此我使用的是多变量LSTM.问题是存在一些缺失值,例如:
Feature 1 Feature 2 ... Feature n
1 2 4 nan
2 5 8 10
3 8 8 5
4 nan 7 7
5 6 nan 12
Run Code Online (Sandbox Code Playgroud)
而不是插入缺失值,这可能会在结果中引入偏差,因为有时在同一个特征上有很多连续的时间戳和缺失值,我想知道是否有办法让LSTM学习缺失值,例如,使用掩蔽层或类似的东西?有人可以向我解释一下处理这个问题的最佳方法是什么?我正在使用Tensorflow和Keras.
我正在 tensorflow(2.3) 中学习 keras API。在tensorflow 网站上的本指南中,我找到了一个自定义损失函数的示例:
def custom_mean_squared_error(y_true, y_pred):
return tf.math.reduce_mean(tf.square(y_true - y_pred))
Run Code Online (Sandbox Code Playgroud)
在reduce_mean这个自定义损失函数函数会返回一个标量。
这样定义损失函数合适吗?据我所知,y_true和形状的第一维y_pred是批量大小。我认为损失函数应该为批次中的每个样本返回损失值。所以损失函数应该给出一个形状数组(batch_size,)。但是上面的函数为整个批次提供了一个值。
也许上面的例子是错误的?任何人都可以在这个问题上给我一些帮助吗?
ps为什么我认为损失函数应该返回一个数组而不是单个值?
我阅读了Model类的源代码。当您向方法提供损失函数(请注意它是一个函数,而不是损失类)时Model.compile(),该损失函数用于构造一个LossesContainer对象,该对象存储在Model.compiled_loss. 传递给LossesContainer类的构造函数的这个损失函数再次用于构造一个LossFunctionWrapper对象,该对象存储在LossesContainer._losses.
根据LossFunctionWrapper类的源代码,训练批次的整体损失值是通过LossFunctionWrapper.__call__()方法(继承自Loss类)计算的,即它返回整个批次的单个损失值。但是第LossFunctionWrapper.__call__()一个调用该LossFunctionWrapper.call()方法以获得训练批次中每个样本的损失数组。然后将这些损失最后平均以获得整批的单个损失值。这是在LossFunctionWrapper.call()方法的损失函数提供给Model.compile()方法被调用。
这就是为什么我认为自定义损失函数应该返回一系列损失,而不是单个标量值。此外,如果我们Loss为Model.compile()方法编写自定义类call(),我们自定义Loss类的方法也应该返回一个数组,而不是一个信号值。
我在github上打开了一个问题。已确认需要自定义损失函数来为每个样本返回一个损失值。该示例将需要更新以反映这一点。
machine-learning keras tensorflow loss-function tensorflow2.0
我是laravel的新手.我正在使用Ubuntu 15.04.我使用composer和一个使用$ sudo apt-get install lamp-server^命令的灯服务器安装了Laravel Framework版本5.1.7(LTS)(我没有安装Homestead).我PhpStorm 8.0.3用作IDE.
我创建了三条路线和一个控制器.该PagesController.php文件如下所示:
class PagesController extends Controller
{
public function index()
{
return 'Welcome to my homepage!';
}
public function about()
{
return 'Learn a little about me.';
}
public function hello()
{
return 'Hello World!';
}
}
Run Code Online (Sandbox Code Playgroud)
和routes.php看起来像这样:
Route::get('/', 'PagesController@index');
Route::get('about', 'PagesController@about');
Route::get('hello', 'PagesController@hello');
Run Code Online (Sandbox Code Playgroud)
每当我去http://localhost:63342/my-first-app/public/(或http://localhost:63342/my-first-app/public/index.php)它工作正常并向我显示Welcome to my homepage!消息.但每当我去http://localhost:63342/my-first-app/public/hello或者http://localhost:63342/my-first-app/public/about,我得到的是404 Not Found消息.
此外,.htaccess位于此处的文件 …
我正在尝试在序列中扩展匹配匹配算法.我的比赛长20个单位,每个时间点有4个频道.我已经构建了一个封装匹配的模型,我无法弄清楚如何在滑动窗口中使用它来跨更长的序列应用它来查找序列中的匹配.
我有2个(20, 4)输入张量(query和target),我连接,添加,展平,然后应用一个简单的密集层.我在这个阶段有数据来训练100K查询,目标对.
def sum_seqs(seqs):
return K.sum(seqs, axis=3)
def pad_dims(seq):
return K.expand_dims(seq, axis=3)
def pad_outshape(in_shape):
return (in_shape[0], in_shape[1], in_shape[2], 1)
query = Input((20, 4))
query_pad = Lambda(pad_dims, output_shape=pad_outshape, name='gpad')(query)
target = Input((20,4))
target_pad = Lambda(pad_dims, output_shape=pad_outshape)(target)
matching = Concatenate(axis = 3)([query_pad, target_pad])
matching = Lambda(sum_seqs)(matching)
matching = Flatten()(matching)
matching = Dropout(0.1)(matching)
matching = Dense(1, activation = 'sigmoid')(matching)
match_model = Model([query, target], matching)
Run Code Online (Sandbox Code Playgroud)
这非常有效.现在我想使用这个预先训练的模型来搜索target具有不同query序列的更长序列.
它似乎应该是这样的:
long_target = Input((100, 4))
short_target = …Run Code Online (Sandbox Code Playgroud) 我以为mask_zero=True当输入值为0时将输出0,因此以下各层可能会跳过计算或其他操作。
如何mask_zero运作?
例:
data_in = np.array([
[1, 2, 0, 0]
])
data_in.shape
>>> (1, 4)
# model
x = Input(shape=(4,))
e = Embedding(5, 5, mask_zero=True)(x)
m = Model(inputs=x, outputs=e)
p = m.predict(data_in)
print(p.shape)
print(p)
Run Code Online (Sandbox Code Playgroud)
实际输出为:(数字是随机的)
(1, 4, 5)
[[[ 0.02499047 0.04617121 0.01586803 0.0338897 0.009652 ]
[ 0.04782704 -0.04035913 -0.0341589 0.03020919 -0.01157228]
[ 0.00451764 -0.01433611 0.02606953 0.00328832 0.02650392]
[ 0.00451764 -0.01433611 0.02606953 0.00328832 0.02650392]]]
Run Code Online (Sandbox Code Playgroud)
但是,我认为输出将是:
[[[ 0.02499047 0.04617121 0.01586803 0.0338897 0.009652 ]
[ 0.04782704 -0.04035913 -0.0341589 0.03020919 …Run Code Online (Sandbox Code Playgroud) 我正在尝试在TensorFlow代码中使用预先训练的Keras模型,如本节Keras博客文章第II部分:使用带有TensorFlow的Keras模型中所述.
我想使用Keras中提供的预先训练的VGG16网络从图像中提取卷积特征图,并在其上添加我自己的TensorFlow代码.所以我做到了这一点:
import tensorflow as tf
from tensorflow.python.keras.applications.vgg16 import VGG16, preprocess_input
from tensorflow.python.keras import backend as K
# images = a NumPy array containing 8 images
model = VGG16(include_top=False, weights='imagenet')
inputs = tf.placeholder(shape=images.shape, dtype=tf.float32)
inputs = preprocess_input(inputs)
features = model(inputs)
with tf.Session() as sess:
K.set_session(sess)
output = sess.run(features, feed_dict={inputs: images})
print(output.shape)
Run Code Online (Sandbox Code Playgroud)
但是,这给了我一个错误:
FailedPreconditionError: Attempting to use uninitialized value block1_conv1_2/kernel
[[Node: block1_conv1_2/kernel/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](block1_conv1_2/kernel)]]
[[Node: vgg16_1/block5_pool/MaxPool/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_132_vgg16_1/block5_pool/MaxPool", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Run Code Online (Sandbox Code Playgroud)
相反,如果我在运行网络之前运行初始化程序操作:
with …Run Code Online (Sandbox Code Playgroud) 我目前正在使用Keras模型,该模型具有嵌入层作为第一层.为了可视化彼此之间的单词的关系和相似性,我需要一个函数来返回词汇表中每个元素的单词和向量的映射(例如'love' - [0.21,0.56,...,0.65,0.10] ).
有什么办法吗?
假设我有以下参数的网络:
现在,我知道损失是按以下方式计算的:二进制交叉熵应用于图像中关于每个类的每个像素.基本上,每个像素将有5个损耗值
这一步后会发生什么?
当我训练我的网络时,它只为一个纪元打印一个损失值.在生成单个值时需要进行多种级别的损失累积,以及在文档/代码中它的发生方式根本不明确.
axis=-1.那么这是所有类的所有像素的平均值还是所有类的平均值,还是两者都是?以不同的方式说明:不同类别的损失如何组合以产生图像的单一损失值?
这完全没有在文档中解释,对于对keras进行多类预测的人来说非常有用,无论网络类型如何.这是keras代码开始的链接,其中一个首先通过了损失函数.
我能找到最接近解释的是
loss:String(目标函数的名称)或目标函数.看到损失.如果模型具有多个输出,则可以通过传递字典或损失列表在每个输出上使用不同的损失.然后,模型将最小化的损失值将是所有单个损失的总和
来自keras.那么这是否意味着图像中每个类的损失只是总和?
此处的示例代码供有人试用.这是从Kaggle借来的基本实现,并针对多标签预测进行了修改:
# Build U-Net model
num_classes = 5
IMG_DIM = 256
IMG_CHAN = 3
weights = {0: 1, 1: 1, 2: 1, 3: 1, 4: 1000} #chose an extreme value just to check for any reaction
inputs = Input((IMG_DIM, IMG_DIM, IMG_CHAN))
s = Lambda(lambda x: …Run Code Online (Sandbox Code Playgroud) 内容:
我目前正在使用带有Tensorflow后端的Keras进行时间序列预测,因此研究了此处提供的教程。
在学习完本教程之后,我开始fit_generator()描述该方法的生成器。该生成器生成的输出如下(左侧示例,右侧目标):
[[[10. 15.]
[20. 25.]]] => [[30. 35.]] -> Batch no. 1: 2 Samples | 1 Target
---------------------------------------------
[[[20. 25.]
[30. 35.]]] => [[40. 45.]] -> Batch no. 2: 2 Samples | 1 Target
---------------------------------------------
[[[30. 35.]
[40. 45.]]] => [[50. 55.]] -> Batch no. 3: 2 Samples | 1 Target
---------------------------------------------
[[[40. 45.]
[50. 55.]]] => [[60. 65.]] -> Batch no. 4: 2 Samples | 1 Target
---------------------------------------------
[[[50. 55.]
[60. 65.]]] => …Run Code Online (Sandbox Code Playgroud) 我正在通过本教程预测温度来尝试R中的Keras软件包.但是,本教程没有解释如何使用训练有素的RNN模型进行预测,我想知道如何做到这一点.为了训练模型,我使用了从教程中复制的以下代码:
dir.create("~/Downloads/jena_climate", recursive = TRUE)
download.file(
"https://s3.amazonaws.com/keras-datasets/jena_climate_2009_2016.csv.zip",
"~/Downloads/jena_climate/jena_climate_2009_2016.csv.zip"
)
unzip(
"~/Downloads/jena_climate/jena_climate_2009_2016.csv.zip",
exdir = "~/Downloads/jena_climate"
)
library(readr)
data_dir <- "~/Downloads/jena_climate"
fname <- file.path(data_dir, "jena_climate_2009_2016.csv")
data <- read_csv(fname)
data <- data.matrix(data[,-1])
train_data <- data[1:200000,]
mean <- apply(train_data, 2, mean)
std <- apply(train_data, 2, sd)
data <- scale(data, center = mean, scale = std)
generator <- function(data, lookback, delay, min_index, max_index,
shuffle = FALSE, batch_size = 128, step = 6) {
if (is.null(max_index))
max_index <- nrow(data) - delay - 1
i …Run Code Online (Sandbox Code Playgroud) keras ×9
python ×6
tensorflow ×6
lstm ×2
controller ×1
dictionary ×1
generator ×1
keras-layer ×1
laravel-5 ×1
missing-data ×1
php ×1
r ×1
routes ×1
vgg-net ×1