python 异常检测算法_有没有用TensorFlow实现异常检测算法的例子?

下面是一个使用Holt-Winters的顺序过滤的例子。同样的模式也适用于其他类型的序列建模,如Kalman滤波器。在from matplotlib import pyplot

import numpy as np

import tensorflow as tf

tf.logging.set_verbosity(tf.logging.INFO)

seasonality = 10

def model_fn(features, targets):

"""Defines a basic Holt-Winters sequential filtering model in TensorFlow.

See http://www.itl.nist.gov/div898/handbook/pmc/section4/pmc435.htm"""

times = features["times"]

values = features["values"]

# Initial estimates

initial_trend = tf.reduce_sum(

(values[seasonality:2*seasonality] - values[:seasonality])

/ seasonality ** 2)

initial_smoothed_observation = values[0]

# Seasonal indices are multiplicative, so having them near 0 leads to

# instability

initial_seasonal_indices = 1. + tf.exp(

tf.get_variable("initial_seasonal_indices", shape=[seasonality]))

with tf.variable_scope("smoothing_parameters",

initializer=tf.zeros_initializer):

# Trained scalars for smoothing, transformed to be in (0, 1)

observation_smoothing = tf.sigmoid(

tf.get_variable(name="observation_smoothing", shape=[]))

trend_smoothing = tf.sigmoid(

tf.get_variable(name="trend_smoothing", shape=[]))

seasonal_smoothing = tf.sigmoid(

tf.get_variable(name="seasonal_smoothing", shape=[]))

def filter_function(

current_index, seasonal_indices, previous_smoothed_observation,

previous_trend, previous_loss_sum):

current_time = tf.gather(times, current_index)

current_observation = tf.gather(values, current_index)

current_season = current_time % seasonality

one_step_ahead_prediction = (

(previous_smoothed_observation + previous_trend)

* tf.gather(seasonal_indices, current_season))

new_loss_sum = previous_loss_sum + (

one_step_ahead_prediction - current_observation) ** 2

new_smoothed_observation = (

(observation_smoothing * current_observation

/ tf.gather(seasonal_indices, current_season))

+ ((1. - observation_smoothing)

* (previous_smoothed_observation + previous_trend)))

new_trend = (

(trend_smoothing

* (new_smoothed_observation - previous_smoothed_observation))

+ (1. - trend_smoothing) * previous_trend)

updated_seasonal_index = (

seasonal_smoothing * current_observation / new_smoothed_observation

+ ((1. - seasonal_smoothing)

* tf.gather(seasonal_indices, current_season)))

new_seasonal_indices = tf.concat(

concat_dim=0,

values=[seasonal_indices[:current_season],

[updated_seasonal_index],

seasonal_indices[current_season + 1:]])

# Preserve shape to keep the while_loop shape invariants happy

new_seasonal_indices.set_shape(seasonal_indices.get_shape())

return (current_index + 1, new_seasonal_indices, new_smoothed_observation,

new_trend, new_loss_sum)

def while_run_condition(current_index, *unused_args):

return current_index < tf.shape(times)[0]

(_, final_seasonal_indices, final_smoothed_observation, final_trend,

sum_squared_errors) = tf.while_loop(

cond=while_run_condition,

body=filter_function,

loop_vars=[0, initial_seasonal_indices, initial_smoothed_observation,

initial_trend, 0.])

normalized_loss = sum_squared_errors / tf.cast(tf.shape(times)[0],

dtype=tf.float32)

train_op = tf.contrib.layers.optimize_loss(

loss=normalized_loss,

global_step=tf.contrib.framework.get_global_step(),

learning_rate=0.1,

optimizer="Adam")

prediction_times = tf.range(30)

prediction_values = (

(final_smoothed_observation + final_trend * tf.cast(prediction_times,

dtype=tf.float32))

* tf.cast(tf.gather(params=final_seasonal_indices,

indices=prediction_times % seasonality),

dtype=tf.float32))

predictions = {"times": prediction_times,

"values": prediction_values}

return predictions, normalized_loss, train_op

# Create a synthetic time series with seasonality, trend, and a little noise

series_length = 50

times = np.arange(series_length, dtype=np.int32)

values = 5. + (

0.02 * times + np.sin(times * 2 * np.pi / float(seasonality))

+ np.random.normal(size=[series_length], scale=0.2)).astype(np.float32)

# Define an input function to feed the data into our model

input_fn = lambda: ({"times":tf.convert_to_tensor(times, dtype=tf.int32),

"values":tf.convert_to_tensor(values, dtype=tf.float32)},

{})

# Wrap the model in a tf.learn Estimator for training and inference

estimator = tf.contrib.learn.Estimator(model_fn=model_fn)

estimator.fit(input_fn=input_fn, steps=500)

predictions = estimator.predict(input_fn=input_fn, as_iterable=False)

# Plot the training data and predictions

pyplot.plot(range(series_length), values)

pyplot.plot(series_length + predictions["times"], predictions["values"])

pyplot.show()

(我写这篇文章时使用的是TensorFlow 0.11.0rc0)

然而,当扩展到更长的时间序列时,这段代码将相当慢。问题是TensorFlow(和大多数其他自动微分工具)在顺序计算(循环)上没有很好的性能。通常通过批处理数据和在大数据块上操作来改善这一点。对于顺序模型来说,这有点棘手,因为需要将状态从一个时间步传递到下一个时间步。在

一种更快(但可能不太令人满意)的方法是使用自回归模型。这还有一个好处,即在TensorFlow中非常容易实现:

^{pr2}$

注意,由于模型没有状态可保持,我们可以很容易地进行小批量随机梯度下降。在

对于集群,类似k-means的方法可以用于时间序列。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值