import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
import time as time
import tensorflow.keras.preprocessing.image as image
import matplotlib.pyplot as plt
import os
from scipy.io import loadmat
import pandas as pd
import numpy as np
from sklearn import preprocessing # 0-1编码
from sklearn.model_selection import StratifiedShuffleSplit # 随机划分,保证每一类比例相同
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from tensorflow.keras.initializers import glorot_uniform
import matplotlib.pyplot as plt
from prettytable import PrettyTable
import six
import math
from matplotlib.pyplot import MultipleLocator
from sklearn.metrics import classification_report
from matplotlib.font_manager import FontProperties
physical_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
win_size = 6
displacement = 3
mask = tf.zeros((win_size**2, win_size**2))
mask.shape
TensorShape([36, 36])
mask[-displacement*win_size:, :-displacement*win_size] = float('-inf')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in
----> 1 mask[-displacement*win_size:, :-displacement*win_size] = float('-inf')
TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment
float('-inf')
-inf
mask_1 = tf.ones((win_size**2, win_size**2))
mask_1*float('-inf')
mask_1[-displacement*win_size:, :-displacement*win_size]*float('-inf')
def set_value(matrix, x, y, val):
# 提取出要更新的行
row = tf.gather(matrix, x)
# 构造这行的新数据
new_row = tf.concat([row[:y], [val], row[y+1:]], axis=0)
# 使用 tf.scatter_update 方法进正行替换
matrix.assign(tf.scatter_update(matrix, x, new_row))
tf.scatter_nd()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in
----> 1 tf.scatter_nd()
TypeError: scatter_nd() missing 3 required positional arguments: 'indices', 'updates', and 'shape'
t=[[2,3,4],[5,6,7]]
print(tf.pad(t,[[0,2],[0,2]],"CONSTANT"))
tf.Tensor(
[[2 3 4 0 0]
[5 6 7 0 0]
[0 0 0 0 0]
[0 0 0 0 0]], shape=(4, 5), dtype=int32)
mask_1 = tf.ones((win_size**2, win_size**2))
mask_1
mask_2 = tf.ones((displacement*win_size,displacement*win_size))*float('-inf')
tf.pad(mask_2,[[0,win_size**2-displacement*win_size],[0,win_size**2-displacement*win_size]])
mask_3 = tf.pad(mask_2,[[0,win_size**2-displacement*win_size],[0,win_size**2-displacement*win_size]])
t=[[2,3,4],[5,6,7]]
print(tf.pad(t,[[2,0],[0,2]],"CONSTANT"))
tf.Tensor(
[[0 0 0 0 0]
[0 0 0 0 0]
[2 3 4 0 0]
[5 6 7 0 0]], shape=(4, 5), dtype=int32)
mask_4 = tf.pad(mask_2,[[win_size**2-displacement*win_size,0],[0,win_size**2-displacement*win_size]])
mask_4
class CyclicShift(keras.layers.Layer):
def __init__(self, displacement_1,displacement_2):
super().__init__()
self.displacement_1 = displacement_1
self.displacement_2 = displacement_2
def call(self, x):
return tf.roll(x, shift = (self.displacement_1,self.displacement_2), axis=(1,2))
arr1 = np.array([[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]]])
CyclicShift(1,1)(arr1)
[ 3, 1, 2]],
[[12, 10, 11],
[ 9, 7, 8]]])>
class CyclicShift1:
def __init__(self, displacement_1,displacement_2):
self.displacement_1 = displacement_1
self.displacement_2 = displacement_2
def roll_(self, x):
return tf.roll(x, shift = (self.displacement_1,self.displacement_2), axis=(1,2))
CyclicShift1(1,1).roll_(arr1)
[ 3, 1, 2]],
[[12, 10, 11],
[ 9, 7, 8]]])>