import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Activation, BatchNormalization, Layer, Dropout, Add
from tensorflow.keras import activations
from tensorflow.python.keras.initializers import Zeros, TruncatedNormal, random_normal, glorot_uniform
from tensorflow.python.keras.regularizers import l2
import numpy as np
from ..layers.common_layer import LayerNormalization, gelu
class _BaseMultiHeadAttention(Layer):
"""
Base class for two types of Multi-head attention layers:
Self-attention and its more general form used in decoders (the one which
takes values and keys from the encoder).
"""
def __init__(self, num_heads: int, use_masking: bool,
dropout: float = 0.0,
compression_window_size: int = None,
**kwargs):
"""
:param num_heads: number of attention heads
:param use_masking: when True, forbids the attention to see the further
elements in the sequence (particularly important in language
modelling).
:param dropout: dropout that should be applied to the attention
(after the softmax).
:param compression_window_size: an integer value >= 1 controlling
how much we should compress the attention. For more details,
read about memory-compressed self-attention in
"Generating Wikipedia by summarizing long sequences"
(https://arxiv.org/pdf/1801.10198.pdf).
:param kwargs: any extra arguments typical for a Keras layer,
such as name, etc.
"""
self.num_heads = num_heads
self.use_masking = use_masking
self.dropout = dropout
if (compression_window_size is not None
and compression_window_size <= 0):
assert ValueError(
f"Too small compression window ({compression_window_size})")
self.compression_window_size = compression_window_size
super().__init__(**kwargs)
def get_config(self):
config = super().get_config()
config['num_heads'] = self.num_heads
config['use_masking'] = self.use_masking
config['dropout'] = self.dropout
config['compression_window_size'] = self.compression_window_size
return config
# noinspection PyAttributeOutsideInit
def build_output_params(self, d_model):
self.output_weights = self.add_weight(
name='output_weights',
shape=(d_model, d_model),
initializer='glorot_uniform',
trainable=True)
if self.compression_window_size is not None:
self.k_conv_kernel = self.add_weight(
name='k_conv_kernel',
shape=(self.compression_window_size,
d_model // self.num_heads,
d_model // self.num_heads),
initializer='glorot_uniform',
trainable=True)
self.k_conv_bias = self.add_weight(
name='k_conv_bias',
shape=(d_model // self.num_heads,),
initializer='zeros',
trainable=True)
self.v_conv_kernel = self.add_weight(
name='v_conv_kernel',
shape=(self.compression_window_size,
d_model // self.num_heads,
d_model // self.num_heads),
initializer='glorot_uniform',
trainable=True)
self.v_conv_bias = self.add_weight(
name='v_conv_bias',
shape=(d_model // self.num_heads,),
initializer='zeros',
trainable=True)
def validate_model_dimensionality(self, d_model: int):
if d_model % self.num_heads != 0:
raise ValueError(
f'The size of the last dimension of the input '
f'({d_model}) must be evenly divisible by the number'
f'of the attention heads {self.num_heads}')
def attention(self, pre_q, pre_v, pre_k, out_seq_len: int, d_model: int,
training=None):
"""
Calculates the output of the attention once the affine transformations
of the inputs are done. Here's the shapes of the arguments:
:param pre_q: (batch_size, q_seq_len, num_heads, d_model // num_heads)
:param pre_v: (batch_size, v_seq_len, num_heads, d_model // num_heads)
:param pre_k: (batch_size, k_seq_len, num_heads, d_model // num_heads)
:param out_seq_len: the length of the output sequence
:param d_model: dimensionality of the model (by the paper)
:param training: Passed by Keras. Should not be defined manually.
Optional scalar tensor indicating if we're in training
or inference phase.
"""
# shaping Q and V into (batch_size, num_heads, seq_len, d_model//heads)
q = K.permute_dimensions(pre_q, [0, 2, 1, 3])
v = K.permute_dimensions(pre_v, [0, 2, 1, 3])
if self.compression_window_size is None:
k_transposed = K.permute_dimensions(pre_k, [0, 2, 3, 1])
else:
# Memory-compressed attention described in paper
# "Generating Wikipedia by Summarizing Long Sequences"
# (https://arxiv.org/pdf/1801.10198.pdf)
# It compresses keys and values using 1D-convolution which reduces
# the size of Q * K_transposed from roughly seq_len^2
# to convoluted_seq_len^2. If we use strided convolution with
# window size = 3 and stride = 3, memory requirements of such
# memory-compressed attention will be 9 times smaller than
# that of the original version.
if self.use_masking:
raise NotImplementedError(
"Masked memory-compressed attention has not "
"been implemented yet")
k = K.permute_dimensions(pre_k, [0, 2, 1, 3])
k, v = [
K.reshape(
# Step 3: Return the result to its original dimensions
# (batch_size, num_heads, seq_len, d_model//heads)
K.bias_add(
# Step 3: ... and add bias
K.conv1d(
# Step 2: we "compress" K and V using strided conv
K.reshape(
# Step 1: we reshape K and V to
# (batch + num_heads, seq_len, d_model//heads)
item,
(-1,
K.int_shape(item)[-2],
d_model // self.num_heads)),
kernel,
strides=self.compression_window_size,
padding='valid', data_format='channels_last'),
bias,
data_format='channels_last'),
# new shape
K.concatenate([
K.shape(item)[:2],
[-1, d_model // self.num_heads]]))
for item, kernel, bias in (
(k, self.k_conv_kernel, self.k_conv_bias),
(v, self.v_conv_kernel, self.v_conv_bias))]
k_transposed = K.permute_dimensions(k, [0, 1, 3, 2])
# shaping K into (batch_size, num_heads, d_model//heads, seq_len)
# for further matrix multiplication
sqrt_d = K.constant(np.sqrt(d_model // self.num_heads),
dtype=K.floatx())
q_shape = K.int_shape(q)
k_t_shape = K.int_shape(k_transposed)
v_shape = K.int_shape(v)
# before performing batch_dot all tensors are being converted to 3D
# shape (batch_size * num_heads, rows, cols) to make sure batch_dot
# performs identically on all backends
attention_heads = K.reshape
没有合适的资源?快使用搜索试试~ 我知道了~
BiLSTM深度学习房价预测
共98个文件
pyc:32个
py:18个
csv:13个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 197 浏览量
2023-04-01
22:41:04
上传
评论 1
收藏 28.93MB ZIP 举报
温馨提示
BiLSTM深度学习房价预测 attention_BiRNN attention_BiRNN.py attention_layers.py capsule_layers.py common_layer.py ctr_layers.py embedding_layers.py __init__.py 以下是文件夹 __pycache__ 里的文件 attention_BiRNN.cpython-36.pyc attention_layers.cpython-36.pyc capsule_layers.cpython-36.pyc common_layer.cpython-36.pyc ctr_layers.cpython-36.pyc ctr_layers.cpython-37.pyc embedding_layers.cpython-36.pyc __init__.cpython-36.pyc __init__.cpython-37.pyc lr.py model.py xdeepfm.py __init__.py 以下是文件夹 __pycache__ 里的文件 fm.cpython-
资源推荐
资源详情
资源评论
收起资源包目录
深度学习房价预测.zip (98个子文件)
universal_algorithm
universal_algorithm
output
.idea
workspace.xml 6KB
universal_algorithm.iml 673B
misc.xml 188B
inspectionProfiles
Project_Default.xml 652B
profiles_settings.xml 174B
modules.xml 297B
.gitignore 50B
work_order
main.py 9KB
test.xls 58KB
algorithm
__init__.py 0B
layers
__init__.py 0B
attention_BiRNN.py 4KB
embedding_layers.py 11KB
common_layer.py 2KB
ctr_layers.py 16KB
attention_layers.py 18KB
__pycache__
common_layer.cpython-36.pyc 2KB
attention_BiRNN.cpython-36.pyc 3KB
embedding_layers.cpython-36.pyc 12KB
ctr_layers.cpython-36.pyc 14KB
__init__.cpython-37.pyc 162B
ctr_layers.cpython-37.pyc 10KB
capsule_layers.cpython-36.pyc 7KB
attention_layers.cpython-36.pyc 13KB
__init__.cpython-36.pyc 150B
capsule_layers.py 9KB
toolbox
__init__.py 0B
ctr_box.py 3KB
__pycache__
ctr_box.cpython-37.pyc 593B
__init__.cpython-37.pyc 163B
ctr_box.cpython-36.pyc 3KB
__init__.cpython-36.pyc 151B
datasets
tabular_dataset.py 6KB
__init__.py 0B
dataset.py 1KB
__pycache__
dataset.cpython-37.pyc 1KB
dataset.cpython-36.pyc 2KB
tabular_dataset.cpython-36.pyc 4KB
tabular_dataset.cpython-37.pyc 3KB
__init__.cpython-37.pyc 164B
tabular_dataset_mtl.cpython-37.pyc 4KB
__init__.cpython-36.pyc 152B
models
__init__.py 0B
lr.py 8KB
model.py 5KB
xdeepfm.py 2KB
__pycache__
xdeepfm.cpython-36.pyc 2KB
lr.cpython-37.pyc 3KB
fm.cpython-37.pyc 773B
lr_mtl.cpython-37.pyc 4KB
model.cpython-36.pyc 5KB
lr.cpython-36.pyc 6KB
__init__.cpython-37.pyc 162B
model.cpython-37.pyc 3KB
xdeepfm_mtl.cpython-37.pyc 1KB
__init__.cpython-36.pyc 150B
xdeepfm.cpython-37.pyc 1KB
__pycache__
__init__.cpython-36.pyc 151B
train.xls 58KB
modeling
train.tfrecord 46KB
schema_dict.json 12KB
test.tfrecord 46KB
model
model.ckpt-2.index 2KB
model.ckpt-6.meta 321KB
checkpoint 210B
model.ckpt-4.data-00000-of-00001 3.73MB
graph.pbtxt 790KB
model.ckpt-4.meta 321KB
model.ckpt-6.index 2KB
events.out.tfevents.1648296116.DESKTOP-94O5NC1 1.1MB
model.ckpt-2.data-00000-of-00001 3.73MB
events.out.tfevents.1648296008.DESKTOP-94O5NC1 241KB
model.ckpt-0.meta 321KB
model.ckpt-4.index 2KB
events.out.tfevents.1648296151.DESKTOP-94O5NC1 1.1MB
model.ckpt-0.data-00000-of-00001 3.73MB
events.out.tfevents.1648295981.DESKTOP-94O5NC1 1.1MB
model.ckpt-2.meta 321KB
model.ckpt-0.index 2KB
model.ckpt-6.data-00000-of-00001 3.73MB
serving
my_data
Untitled-1.ipynb 13MB
gongshu杭州二手房.csv 2.82MB
yuhang杭州二手房.csv 2.76MB
shangcheng杭州二手房.csv 2.66MB
fuyang杭州二手房.csv 1.69MB
2021年杭州二手房预测cdata.csv 17.54MB
xiaoshan杭州二手房.csv 2.68MB
xihu杭州二手房.csv 2.78MB
qiantangqu杭州二手房.csv 2.68MB
linan杭州二手房.csv 2.44MB
杭州房价各区域单价分布.png 1.01MB
linpingqu杭州二手房.csv 2.63MB
杭州2020和2021二手房分布热度.png 933KB
binjiang杭州二手房.csv 2.76MB
result
WordCloud-热门楼盘.html 975KB
杭州二手房2020年数据.csv 3.48MB
Calendar-2021年杭州二手房单价.html 37KB
杭州二手房2021年数据.csv 13.04MB
共 98 条
- 1
资源评论
- m0_748939332023-12-12资源太好了,解决了我当下遇到的难题,抱紧大佬的大腿~
AI信仰者
- 粉丝: 1w+
- 资源: 143
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功