File size: 6,677 Bytes
ae92d51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# -*- coding: utf-8 -*
# from __future__ import print_function
import sys
import tensorflow as tf
# import tensorflow_datasets as tfds
import numpy as np
import json

tf.enable_eager_execution()

def test():
    # mirrored_strategy = tf.distribute.MirroredStrategy()
    # # 在config中加入镜像策略
    # config = tf.estimator.RunConfig(train_distribute=mirrored_strategy, eval_distribute=mirrored_strategy)
    # 把config加到模型里
    regressor = tf.estimator.LinearRegressor(
        feature_columns=[tf.feature_column.numeric_column('feats')],
        optimizer='SGD'
        # ,config=config
    )
    def input_fn():
        dataset = tf.data.Dataset.from_tensors(({"feats":[1.]}, [1.]))
        return dataset.repeat(1000).batch(10)

    # 正常训练,正常评估
    regressor.train(input_fn=input_fn
                    , steps=20
                    )
    regressor.evaluate(input_fn=input_fn
                       # , steps=10
                       )

def parse_from_json(config_path):
    """ parse feature columns from feature config path

    Args:
      config_path: string, a feature config path
    """
    total = 0
    correct = 0
    with open(config_path, "r") as f:
        config = json.load(f)

    feature_names = set()
    features = config["features"]
    for feature in features:
        feature_name = feature['feature_name']
        if '#' in feature_name:
            feature_name = feature_name.split('#')[0]
        feature_names.add(feature_name)
    return feature_names

#convert model's format from *.pb to *.pbtxt
def parse_model_2_txt(saved_model_dir ,output_file):
    from tensorflow.python.saved_model import loader_impl
    from google.protobuf import text_format
    saved_model = loader_impl._parse_saved_model(saved_model_dir)
    with open(output_file, 'w') as f:
        f.write(text_format.MessageToString(saved_model))

# parse_model_2_txt('/Users/machi/git/internal/starship_galaxy/model_zoo/scheduler/2022q2combo/old', '/Users/machi/git/internal/starship_galaxy/model_zoo/scheduler/2022q2combo/old/saved_model.pbtxt')

import os
def build_serving_input_new():
    import pickle
    with tf.gfile.Open('feature_desc.pkl', mode='rb') as f:
        feature_dec = pickle.load(f)
    sep_placeholder = {}
    for name, desc in feature_dec.items():
        if 'sg_poi_click_time_gap_seq_2d' in name:
            print(desc)

    # return sep_placeholder

def read_schema(file):
    d = {}
    with open(file) as f:
        for line in f:
            line = line.strip()
            fds = line.split(' ')
            d[fds[0]] = fds[1]
    return d



def sparse_tensor():
    indices_tf = tf.constant([[0, 0], [0, 1], [1, 1], [2, 2]], dtype=tf.int64)
    values_tf = tf.constant([1, 2, 3, 4], dtype=tf.int32)
    dense_shape_tf = tf.constant([3, 3], dtype=tf.int64)

    sparse_tf = tf.SparseTensor(indices=indices_tf,
                                values=values_tf,
                                dense_shape=dense_shape_tf)
    dense_tf = tf.sparse_tensor_to_dense(sparse_tf)

    # print(dense_tf)


    user_tf = tf.constant([1, 2, 3], dtype=tf.int32, shape=[3, 1])

    # 一行为一个session,每一行包含不同个数的样本。以下示例中,共有3个session,第1个session包含3个样本,第2个session包含2个样本,第3个session行包含1个样本
    # b为non_common特征
    b = tf.constant([[1, 2, 1], [0, 3, 2], [0, 0, 4]])

    # a为common特征,3个session有3个值
    a = tf.constant([1, 2, 3], shape=[3, 1])

    # 将a扩展为和b相同维度
    a = tf.tile(a, tf.constant([1, 3]))
    print(a)

    # 获取b中非0元素的下标
    indices = tf.where(tf.not_equal(b, 0))
    print(indices)

    # 将非0元素的下标处的a和b的值拼接起来,即样本展开后的结果
    c = tf.concat(values=[tf.expand_dims(tf.gather_nd(a, indices), axis=1), tf.expand_dims(tf.gather_nd(b, indices), axis=1)], axis=1)
    print(c)


def kkv_attention(query, key, value, mask=None):
    # Transpose key and value matrices
    key_transpose = tf.transpose(key, perm=[0, 2, 1])
    value_transpose = tf.transpose(value, perm=[0, 2, 1])

    # Compute dot product between query and key
    logits = tf.matmul(query, key_transpose)

    # Apply mask (if provided) to logits
    if mask is not None:
        logits += mask

    # Apply softmax activation to obtain attention scores
    attention_scores = tf.nn.softmax(logits, axis=-1)

    # Apply attention scores to value to obtain context vector
    context_vector = tf.matmul(attention_scores, value_transpose)

    # Transpose back the output
    context_vector = tf.transpose(context_vector, perm=[0, 2, 1])

    return context_vector, attention_scores

# write kkv attention function
def write_kkv_attention(query, key, value, mask=None):
    # Transpose key and value matrices
    # key_transpose = tf.transpose(key, perm=[0, 2, 1])
    # value_transpose = tf.transpose(value, perm=[0, 2, 1])

    # Compute dot product between query and key
    logits = tf.matmul(query, key)

    # Apply mask (if provided) to logits
    if mask is not None:
        logits += mask

    # Apply softmax activation to obtain attention scores
    attention_scores =   tf.nn.softmax(logits, axis=-1)

    # Apply attention scores to value to obtain context vector
    context_vector =  tf.matmul(attention_scores, value)

    # Transpose back the output
    # context_vector =  tf.transpose(context_vector, perm=[0, 2, 1])

    return context_vector, attention_scores

# test write_kkv_attention
def test_write_kkv_attention():
    # define query and key matrices
    query =  tf.constant([[-0.1250,  0.0000, -0.5000,  0.5000,  0.0000]])

    key =  tf.constant([[ -0.1250,  0.0000, -0.5000,  0.5000,  0.0000],
                        [-0.5000,  0.0000,  0.5000,  0.5000,  0.0000],
                        [-0.2500, -0.5000,  0.0000,  0.5000,  0.2500],
                        [ 0.0000,  0.0000,  0.0000,  0.5000,  0.5000],
                        [ 0.5000,  0.5000,  0.0000, -0.5000,  0.5000]])

    value =   tf.constant([[-0.5000,  0.0000,  0.5000,  0.5000,  0.0000],
                        [-0.5000,  0.0000,  0.5000,  0.5000,  0.0000],
                        [-0.5000,  0.0000,  0.5000,  0.5000,  0.0000],
                        [ 0.0000,  0.0000,  0.5000,  0.5000,  0.5000],
                        [ 0.5000,  0.5000,  0.0000, -0.5000,  0.5000]])


    mask = None

    # call write_kkv_attention and obtain context vector and attention scores
    context_vector, attention_scores = write_kkv_attention(query, key, value,mask)

    # print results
    print context_vector
    print attention_scores


print '123', 1