我尝试重现TensorFlow的LSTMCell生成的结果,以确保我知道它的作用.
这是我的TensorFlow代码:
num_units = 3
lstm = tf.nn.rnn_cell.LSTMCell(num_units = num_units)
timesteps = 7
num_input = 4
X = tf.placeholder("float", [None, timesteps, num_input])
x = tf.unstack(X, timesteps, 1)
outputs, states = tf.contrib.rnn.static_rnn(lstm, x, dtype=tf.float32)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
x_val = np.random.normal(size = (1, 7, num_input))
res = sess.run(outputs, feed_dict = {X:x_val})
for e in res:
print e
Run Code Online (Sandbox Code Playgroud)
这是它的输出:
[[-0.13285545 -0.13569424 -0.23993783]]
[[-0.04818152 0.05927373 0.2558436 ]]
[[-0.13818116 -0.13837864 -0.15348436]]
[[-0.232219 0.08512601 0.05254192]]
[[-0.20371495 -0.14795329 -0.2261929 ]]
[[-0.10371902 -0.0263292 -0.0914975 …Run Code Online (Sandbox Code Playgroud) 我想编写一个模板来确定类型是否是编译时的stl容器.
我有以下一点代码:
struct is_cont{};
struct not_cont{};
template <typename T>
struct is_cont { typedef not_cont result_t; };
Run Code Online (Sandbox Code Playgroud)
但我不确定如何创建必要的专业化std::vector<T,Alloc>, deque<T,Alloc>, set<T,Alloc,Comp>等...
我编写了一个由Layer类扩展的自定义图层类,然后我想挑选历史记录以供进一步分析,但是当我从文件重新加载pickle对象时,python引发了一个错误:
未知图层:注意.
那么,我该如何解决呢?
我都一直get_config,__getstate__并且__setstate__,但是失败了.我只想挑选keras历史,但不是模型,所以请不要告诉我带custom_object参数的保存模型方法.
我想n*n在TensorFlow中创建一个对称矩阵并训练这个矩阵.实际上,我应该只训练(n+1)*n/2参数.我该怎么做?
我看到一些以前的线程建议执行以下操作:
X = tf.Variable(tf.random_uniform([d,d], minval=-.1, maxval=.1, dtype=tf.float64))
X_symm = 0.5 * (X + tf.transpose(X))
Run Code Online (Sandbox Code Playgroud)
但是,这意味着我必须训练n*n变量,而不是n*(n+1)/2变量.
即使没有实现这一功能,一段自编代码也会有所帮助!
谢谢!
我正在尝试制作一个解决难题的程序.我的尝试与我测试的样本拼图一起工作得很好.现在我正在尝试制作一个真正的拼图.这个新拼图的拼图块并没有真正的形状.
我设法将图像变成黑白,最后变成1和0的数组,其中1表示片段,0表示背景.我想找到一种方法来识别这些碎片的4个角,峰和深度的坐标.
我试图计算1附近的0的数量,以查看边界中的最大曲线.但是它的形状不够平滑,无法工作.
counter = np.zeros((lenX,lenY),dtype=int)
for i in range(lenX):
for j in range(lenY):
if img[i,j]==1:
counter[i,j] = count_white(img,i,j,lenX,lenY)
print(counter)
tpath = os.getcwd()+"/test.jpg"
print(cv2.imwrite(tpath, Image))
print("saved at : ",tpath)
np.savetxt("test.csv", counter, delimiter=",")
def count_white(img,x,y,lenX,lenY):
X = [x-1,x,x+1,x+1,x+1,x,x-1,x-1]
Y = [y-1,y-1,y-1,y,y+1,y+1,y+1,y]
count = 0
for i in range(len(X)):
if X[i] < lenX and Y[i] < lenY:
if img[X[i],Y[i]] == 0:
count=count+1
return count
Run Code Online (Sandbox Code Playgroud)
有任何建议,参考或想法吗?
我试图通过传递我创建的低通滤波器来使用 fft 模糊图像,但输出结果是一个充满灰色噪声的图像。我只是想遵循这里的基础知识,但我的实现似乎有问题:
from scipy import fftpack
import numpy as np
import imageio
from PIL import Image, ImageDraw
image1 = imageio.imread('image.jpg',as_gray=True)
#convert image to numpy array
image1_np=np.array(image)
#fft of image
fft1 = fftpack.fftshift(fftpack.fft2(image1_np))
#Create a low pass filter image
x,y = image1_np.shape[0],image1_np.shape[1]
#size of circle
e_x,e_y=50,50
#create a box
bbox=((x/2)-(e_x/2),(y/2)-(e_y/2),(x/2)+(e_x/2),(y/2)+(e_y/2))
low_pass=Image.new("L",(image1_np.shape[0],image1_np.shape[1]),color=0)
draw1=ImageDraw.Draw(low_pass)
draw1.ellipse(bbox, fill=255)
low_pass_np=np.array(low_pass)
low_pass_fft=fftpack.fftshift(fftpack.fft2(low_pass))
#multiply both the images
filtered=np.multiply(fft1,low_pass_fft)
#inverse fft
ifft2 = abs(fftpack.ifft2(fftpack.ifftshift(filtered)))
#save the image
imageio.imsave('fft-then-ifft.png', ifft2.astype(np .uint8))
Run Code Online (Sandbox Code Playgroud)
我想使用 Conv1D 将扩张卷积网络制作为顺序数据集。
所以我尝试使用 Boston 数据集进行 Conv1D。
from tensorflow.python.keras.layers import Conv1D, MaxPooling2D
from tensorflow.python.keras.layers import Activation, Dropout, Flatten, Input, Dense
from tensorflow.python.keras.models import Model
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
boston = load_boston()
df = pd.DataFrame(boston.data,columns=boston.feature_names)
df['target']= boston.target
y = df['target']
X = df.drop(columns=['target'])
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=2)
def NN_model(data= X_train):
#data = np.expand_dims(data, axis=2)
inputs = Input(((data.shape)))
x = Conv1D(2,2,dilation_rate=2,padding="same", activation="relu")(inputs)
x = Flatten()(x)
x = Dense(2048, activation="relu")(x)
predictions = Dense(1)(x) …Run Code Online (Sandbox Code Playgroud)