我正在尝试将下面的代码转换为tensorflow。我对tensorflow很陌生,我想知道下面的代码中是否有任何部分可以在tensorflow中更快地运行?有没有办法在张量流中完成相同的事情,并使其运行更快?例如,像最终的点产品一样,像matmul那样效果会更好吗?
代码:
# creates dataframe with columns from x_df and index from x_df
astk_df = pd.DataFrame(columns=x_df.columns, index=x_df.index)
for i, m in enumerate(astk_df.columns):
# creates array of values from astk_df
x = x_df[m].values
# appends 0.0 to the beginning of array
x_extend = np.concatenate((np.zeros(L-1), x)) #we extend the media spend series with zeros to
#compute initial adstock values
# sets weights equal to final alpha value raised to a range of powers
weights = alpha[i]**(np.arange(0, L))
weights /= weights.sum() #normalization constant
for t, week in enumerate(astk_df.index):
astk_df.loc[week, m] = x_extend[t:(t+L)][::-1].dot(weights)