《python深度学习》第八章:生成式深度学习

8.1 使用LSTM生成文本

给定一个序列,预测下一个或多个标记的概率,从而生成之后的序列
给定前面的标记,能够对下一个标记的概率进行建模的任何网络都叫做语言模型
8.1.1 生成式循环网络简史
8.1.2 如何生成序列数据
8.1.3 采样策略的重要性
(以上几小节都不太重要,了解即可)

8.1.4 实现字符级别的LSTM文本生成

# 准备下载语料,并将其转换为小写import keras
import numpy as nppath = keras.utils.get_file('nietzsche.txt', origin = 'https://s3.amazonaws.com/text-datasets/nietzsche.txt')
text = open(path).read().lower()
print('Corpus length:',len(text))
Corpus length: 600893
# 提取60个字符组成的序列
maxlen = 60
# 每3个字符采样一个新序列
step = 3
# 保存所提取的序列
sentences = []
# 保存目标(即下一个字符)
next_chars = []for i in range(0,len(text) - maxlen,step):sentences.append(text[i:i + maxlen])next_chars.append(text[i + maxlen])print('Number of sequences:',len(sentences))# 语料中唯一字符组成的列表
chars = sorted(list(set(text)))
print('Unique characters:',len(chars))
char_indices = dict((char,chars.index(char)) for char in chars)print('Vectorization....')
x = np.zeros((len(sentences),maxlen,len(chars)),dtype = np.bool)
y = np.zeros((len(sentences),len(chars)),dtype = np.bool)# 进行one-hot编码
for i,sentence in enumerate(sentences):for t,char in enumerate(sentence):x[i,t,char_indices[char]] = 1y[i,char_indices[next_chars[i]]] = 1
Number of sequences: 200278
Unique characters: 58
Vectorization....
# 2.构建网络from keras import layersmodel = keras.models.Sequential()
model.add(layers.LSTM(128,input_shape = (maxlen,len(chars))))
model.add(layers.Dense(len(chars),activation = 'softmax'))optimizer = keras.optimizers.RMSprop(lr = 0.01)
model.compile(loss = 'categorical_crossentropy',optimizer = optimizer)
# 3.训练语言模型并从中采样
'''
给定一个训练好的模型和一个种子文本片段,我们可以通过重复以下操作来生成新的文本
1.给定目前以生成的文本,从模型中得到下一个字符的概率分布
2.根据某个温度对分布进行重新加权
3.根据重新加权后的分布对下一个字符进行随机采样
4.将新字符添加到文本末尾
'''def sample(preds,temperature = 1.0):preds = np.asarray(preds).astype('float64')preds = np.log(preds)/temperatureexp_preds = np.exp(preds)preds = exp_preds/np.sum(exp_preds)probas = np.random.multinomial(1,preds,1)return np.argmax(probas)
import random
import sysfor epoch in range(1,60):print('epoch:',epoch)
#     将模型先在数据上拟合一次model.fit(x, y,batch_size=128, epochs=1)start_index = random.randint(0, len(text) - maxlen - 1)generated_text = text[start_index: start_index + maxlen]print('--- Generating with seed: "' + generated_text + '"')for temperature in [0.2,0.5,1.0,1.2]:print('------ temperature:', temperature)sys.stdout.write(generated_text)# 从种子文本开始,生成400个字符,并且对目前生成的字符进行one-hot编码for i in range(400):sampled = np.zeros((1, maxlen, len(chars)))for t, char in enumerate(generated_text):sampled[0, t, char_indices[char]] = 1.# 对下一个字符进行采样preds = model.predict(sampled, verbose=0)[0]next_index = sample(preds, temperature)next_char = chars[next_index]generated_text += next_chargenerated_text = generated_text[1:]sys.stdout.write(next_char)sys.stdout.flush()print()
epoch: 1
Epoch 1/1
200278/200278 [==============================] - 216s 1ms/step - loss: 1.6261
--- Generating with seed: "oungest of all
philosophical methods, discovered experimenta"
------ temperature: 0.2
oungest of all
philosophical methods, discovered experimentary and spirit and which a delicate and profacious and the comprehital and strange of the convention of the sense the sense the something of the good and strong the such and the such a man also as the sense and sense of the same the sense and states and condition of the self-comprehend to the sense and the sense and superition of the strange of the sense the such a man also also the such and delica
------ temperature: 0.5
ge of the sense the such a man also also the such and delicate and does of closing in the everything and delicate and stand of the strantical
decerting the alse possibility of a such a conscience and spirit here as swance the into means of feeling regard to the spore in the cruesless mether and could are of the contrany and desiration. the moral, and propared and been the belogiation of inconsideration of the comes to do and also also also and the which a
------ temperature: 1.0
ation of the comes to do and also also also and the which a nectionally
a rehism excession for we and despreat onecessarians pressiatitahe he medion brinds
hidder (write indeveliament is from badn which," ie to delagerable, and those elselvss of
moral uses af, which casy ore ofaye question. the strunce everything purism, oneferenol
partifity, knews cainth uponz of eprech enough, as no lolk not romity and lot bene hror-towment
leving with it to lass comprih
------ temperature: 1.2
ity and lot bene hror-towment
leving with it to lass comprihit, with, aitte, takn his hee thinkical get. the .all onignly alnot astiliog upin
us, when every selo mirstance: lixtral
grayer wild a manifalch religmate if sehking worts--than was eces
not dealverd; -rongial
culthess at readol thaush
say designs: mety hasser vility, his fait withatten sneled oppossibitates, on
to
draen which vivgly in'thation offordtity wes
womank, in to belogcation--without ar
epoch: 2
Epoch 1/1
200278/200278 [==============================] - 209s 1ms/step - loss: 1.5358
--- Generating with seed: "ion gain credit in the dreaming
state? (for in a dream we lo"
------ temperature: 0.2
ion gain credit in the dreaming
state? (for in a dream we love the present an antiet of the contrife and strength the contrible the sentement and in the strength an according the conscience of the senter of the contribient of the stronger the sense an an which is be all the spirit of the present the conscience of the sense of the not be all the great sense of the contresition that the senter the same all the contrife and conscious of the conscience of the
------ temperature: 0.5
ame all the contrife and conscious of the conscience of the now in the masters, and the such the seriousness of the world of the senter our from the have as a some sense of the same hearther of the way the whole all one who are not entager the conceined an abstuence, the too desire the presing the have man before the precisely of the contrible part of the present an attain the aguisted the laster of all the distance of the own whole all it in the words in
------ temperature: 1.0
of all the distance of the own whole all it in the words in account sense sence that flostical
femrey
to metaphysics even that caste whose wor? ibus we say; it is do ar all, hivoriphing in surhils, we partious feeling stretwerly, the re liakse spirit, hose such a batigally itself for the saids anow howing down as
an over
the reasons, in egod ht is
is expecining pra" not the die there is to inevines of his wat , an instance, a realon and called to i most do
------ temperature: 1.2
s of his wat , an instance, a realon and called to i most dognime, music that, anculaise-crlissjulation anyture. the , another. coming, from degecuesfestesies him sooghesifedaking himself tanges science be powerhmed
we for, for which the
danging , fir be gereathy ryak whatever,
=wis3ly
he nebrdivoribs,
-me
hhe as fig their very sinction therefle: over"i
justicr5, antiet of their german its two dispusition an
naw there
only me him for appesing
leses who bro
epoch: 3
Epoch 1/1
200278/200278 [==============================] - 225s 1ms/step - loss: 1.4908
--- Generating with seed: " relegated to the
physiological sciences and to the history "
------ temperature: 0.2relegated to the
physiological sciences and to the history of the seems that it is a sould and the case of the senses of the interpropers and profound the desirated the generally and interpret one may be a such a sense and seems to the sense of the great the experience of the seems to the sense of the self-desire the desire to the desire of the seems to the man in the sense and life the case of the seems to the sense of the seems to the sense of the seems
------ temperature: 0.5
he seems to the sense of the seems to the sense of the seems that is for the effect the same the attained that it is a cromany and delight of the seems that it is every times as a perhaps that is a sould the act of the sperition, and manifest of the most place because one may law been the sense to his happered with the proper sould the
conceptions of the serient and philosophers and seems to it is not to say, a take allain of the man, which is an arts pres
------ temperature: 1.0not to say, a take allain of the man, which is an arts pressent, now that treatness
madent it were god of things are.himself, in all if errod;
as de hithoronding to repudiing that the intention, your difference of distient regreeture,
and beg(" reveenists ofimented
effords supremis so expression other. which when how of thith turnerak
dinnolly; his only one cimpleniroussifuld aramone, to mander
great the
origin waits highers
attain killer in altely
------ temperature: 1.2
nder
great the
origin waits highers
attain killer in altely is "cruely, (this persiaby dountperered vent. this bouppositionly over, that it a no,
whosepraticl, somewhical. eashly, artaut, which
hen
times as list this has to leastant, and
metno, onem not de, stands befarcodes only theraby too animich honerse realing to mediome uts
meansioni- as; were leves it is --all interistipm
for so as nevererwaid,
as primitivilies givts end-let us. sfrighting tley reg
epoch: 4
Epoch 1/1
200278/200278 [==============================] - 222s 1ms/step - loss: 1.4611
--- Generating with seed: "longs, and as an expression of german taste at a
time when t"
------ temperature: 0.2
longs, and as an expression of german taste at a
time when the existence of the selfled the experience of the selfle a perhaps the free more and some soul in the special and all the selfling the soul--in the same the special man in the problem of the spiritual for the experience of the sense of the spiritual to the same the special and some soul of the suffering in the spould the experience of the spore the special and morality of the spiritual and the hav
------ temperature: 0.5spore the special and morality of the spiritual and the have all the world a manifest to the man even in rank and will of in himself to the spiritual strength, which is selfle to a religious party and the free
specionations of the faction to the spiritual and the even with is understemply and some heals of the emotions of woman that the intellikent as it is things and with a man like a former be sense of all the believe and and in the special even of the
------ temperature: 1.0
sense of all the believe and and in the special even of the himseverable had all limes as spiritual, the spaciously, the promely he even metaphysical bouther, the refentious selfar do on throughllor that s  ze well called our wilithe the exerminite
fatily he hechmuch like man, the adsocial funmed, the leise with it was immediaily for the time
on the interial.
the gecioligo,
the reads clet weile of man, the perhaps, afetenso. to man lawss
and repleation.------ temperature: 1.2man, the perhaps, afetenso. to man lawss
and repleation.huthe, noiss!--deto
our glaumuncors, armance. their virtue for civilizatic prestroisened. the gain, pires, it angulate celsedy mad the eyar natural as he much its conscience of fack and thon
on the bration deipone) this "expeciater,--lenly deluced nears there even spring
endurtity
hows anetwility the indupling
as it, i pleasures emped lavioring
that too had upon vicionscy for the feelings
of
conte
epoch: 5
Epoch 1/1
200278/200278 [==============================] - 221s 1ms/step - loss: 1.4399
--- Generating with seed: "e know well enough how offensive it sounds when any one
plai"
------ temperature: 0.2
e know well enough how offensive it sounds when any one
plain in the sense the interest of the strength of the contrible of the present of the strength of the sense of the sense the strength of the sense the scientific and morality of the sense of the strength of the sense of the sense of the sense of the sense of the strength and condition of the present and problem of the sense to the our interest of the morality of the sense and the sense the strength o
------ temperature: 0.5
st of the morality of the sense and the sense the strength of the moral with a forms of the same to means of delunive and promise for an actionary there is a was the nother what as a man of the present in goddent of the contempt. the warter, when the dorbine case a soul is the science to the specially man we may be soul and the experience of the presented the self-consequently with the spirit, of self-comprehenses the spirit, as a pretance this neithery, w
------ temperature: 1.0
self-comprehenses the spirit, as a pretance this neithery, with
this by the same," whiles which is
enlagitly: belong of great would imparietice, pomettibiesm, philosopher the also connerie of mean unden and to-to metaphysicalibity, it devlloming of
say if, into a rasic himself and carry
grant!--and of
the now? and must happines
of a know noble.
:tygered and succe, and greatness dangerous, his possibility impulstisns its. the dwarties ly alroy lough
strengt
------ temperature: 1.2
ibility impulstisns its. the dwarties ly alroy lough
strengthul, p

可见,较小的温度值会得到极端重复的和可预测的文本,但局部结构是非常真实的,特别是所有单词都是正确的英文单词
随着温度的越来越大,生成的文本也变得更有趣,甚至更具有创造性,它有时会创造出全新的单词,并且看起来很可信。
对于较大的温度值,局部模式开始分解,大部分单词看起来像半随机的字符串。

小结

1.我们可以生成离散的序列数据,其方法是:给定前面的标记,训练一个模型来预测接下来的一个或多个标记

2.对于文本来说,这种模型叫做语言模型,它可以是单词级别的,也可以是字符级别的

3.对下一个标记进行采样,需要在坚持模型的判断与引入随机性之间寻找平衡

4.处理这种问题的一个方法是使用softmax温度,一定要尝试多种不同的温度,以找到合适的哪一个

8.2 DeepDream

DeepDream是一种艺术性的图像修改技术,与第五章的卷积神经网络过滤器可视化技术几乎相同,但有以下几点区别

1.使用DeepDream,我们尝试将所有层的激活最大化,而不是将某一层的激活最大化,因此需要同时将大量特征的可视化混合在一起

2.不是从空白的,略微带有噪声的输入开始,而是从现有的图像开始,因此所产生的效果能够抓住已经存在的视觉模式,并以某种艺术性的方式将图像元素扭曲

3.输入图象是在不同的尺度上(叫做八度)进行处理的,者可以提高可视化的质量

# 使用keras来实现deepdream,我们使用一个预训练过的卷积神经网络,例如VGG16,VGG19,Xception等from keras.applications import inception_v3
from keras import backend as K# 禁用所有与训练有关的操作,因为我们不用训练模型
K.set_learning_phase(0)
# 构建不包括全连接层的Inception V3 网络,使用预训练的imagenet权重来加载模型
model = inception_v3.InceptionV3(weights = 'imagenet',include_top = False)
# 这个字典将层的名称映射为一个系数,这个系数定量表示该层激活对你要最大化的损失的贡献大小。
# 注意,层的名称硬编码在内置的Inception V3应用中,可使用summary函数查看
layer_contributions = {'mixed2':0.2,'mixed3':3.,'mixed4':2.,'mixed':1.5
}
# 定义一个损失函数,就是上边挑选出来的层的激活值的l2范数的加权求和layer_dic = dict([(layer.name,layer) for layer in model.layers])# 在定义损失时,将层的贡献添加到这个标量变量中
loss = K.variable(0.)for layer_name in layer_contributions:coeff = layer_contributions[layer_name]activation = layer_dict[layer_name].outputscaling = K.prod(K.cast(K.shape(activation),'float32'))
#     将该层的l2范数添加到loss中,为了避免出现边界伪影,损失中仅包含非边界的像素loss += coeff * K.sum(K.square(activation[:,2:-2,2:-2,:]))/ scaling
# 设置梯度上升的过程# 这个张量用于保存生成的图像(梦境图片)
dream = model.input# 计算损失相对于梦境图像的梯度
grads = K.gradients(loss,dream)[0]# 将梯度标准化(重要技巧)
grads /= K.maximum(K.mean(K.abs(grads)),le-7)# 给定一张输入图像,设置一个keras函数来获取损失值和梯度值
outputs = [loss,grads]
fetch_loss_and_grads = K.function([dream],outputs)def eval_loss_and_grads(x):outs = fetch_loss_and_grads([x])loss_value = outs[0]grad_values = outs[1]return loss_value,grad_values# 这个函数运行,iterations次梯度上升
def gradient_ascent(x,iterations,step,max_loss =None):for i in range(iterations):loss_value,grad_values = eval_loaa_and_grads(x)if max_loss is not None and loss_value > max_loss:breakprint('...Loss value at ',i,':',loss_value)x += step * grad_valuesreturn x
# 辅助函数
import scipy
from keras.preprocessing import imagedef resize_img(img,size):img = np.copy(img)factors = (1,float(size[0])/img.shape[1],float(size[1])/img.shape[2],1)return scipy.ndimage.zoom(img,factors,order = 1)def svae_img(img,fname):pil_img = deprocess_image(np.copy(img))scipy.misc.imsave(fname,pil_img)# 通用函数,用于打开图像,改变图像大小以及将图像格式转换为Inception v3 模型能够处理的张量
def preprocess_image(image_path):img = image.load_img(image_path)img = image.img_to_array(img)img = np.expend_dims(dim,axis = 0)img = inception_v3.preprocess_input(img)return imgdef deprocess_image(x):if K.image_data_format() == 'channels_first':x = x.reshape((3,x.shape[2],x.shape[3]))x = x.transpose((1,2,0))else:x = x.reshape((x.shape[1],x.shape[2],3))x /= 2x += 0.5x *= 255.x = np.clip(x,0,255),astype('uint8')return x
# 下来定义实际的deepdream算法,首先定义一个列表,里面包含的是处理图像的尺度,
# 每个连续的尺度都是前一个的1.4倍,及首先处理小图像,然后逐渐增大图像尺寸import numpy as np # 改变以下参数可以得到新的结果
# 梯度上升的步长
step = 0.01
# 运行梯度上升的尺度个数
num_octave = 3
# 两个尺度之间的大小比例
octave_scale = 1.4
# 在每个尺度上运行梯度上升的步数
iterations = 20max_loss = 10
base_image_path = r'D:\study\Python\Deeplearning\ch5\sampledata\train\cats\cat.1.jpg'
# 将基础图像加载成一个numpy数组
img = preprocess_image(base_image_path)original_shape = img.shape[1:3]
successive_shapes = [original_shape]
for i in range(1,num_octave):shape = tuple([int(dim/(octave_scale **i)) for dim in original_shape])successive_shapes.append(shape)successive_shapes = successive_shaes[::-1]  # 将形状列表反转,变为升序original_img = np.copy(img)
# 将图像numpy数组的大小缩放到最小尺寸
shrunk_original_img = resize_img(img,successive_shapes[0])for shape in successive_shapes:print('Processing image shape',shape)# 将梦境图像放大img = resize_img(img,shape)
#     运行梯度上升,改变梦境图像img = gradient_ascent(img,iterations = iterations,step = step,max_loss = max_loss)
#     将原始图像的较小版本放大,它会变得像素化upscaled_shrunk_original_img = resize_img(shrunk_original_ims,shape)
#     在这个尺寸上计算原始图像的高质量版本same_size_original = resize_img(original_img,shape)
#     二者的差别就是在放大过程中丢失的细节lost_detail =same_size_original - upscaled_shrunk_original_img#     将丢失的细节重新注入到梦境图片中img += lost_detailshrunk_original_img = resize_img(original_img,shape)save_img(img,fname = 'dream_at_scale_'+ str(shape)+'.png')save_img(img,fname = 'final_dream.png')

小结

1.DeepDream的过程是反向运行一个卷积神经网络,基于网络学到的表示来生成输入

2.得到的结果是很有趣的,有些类似于通过迷幻剂扰乱视觉皮层而诱发的视觉伪影

3.注意,这个过程并不局限于图像模型,甚至并不局限于卷积神经网络,他可以应用于语音,音乐等更多内容

8.3 神经风格迁移

图像风格:指图像中不同空间尺度的纹理,颜色和视觉图案
图像内容:指图像的高级宏观结构

实现的方法:定义一个损失函数,最小化损失函数,损失函数的目标:保存原始图像的内容,同时采用参考图像的风格,将内容和风格在数学上进行定义,就可以得到损失函数

8.3.1 内容损失(只使用一个更靠近顶层的层)

卷积神经网络的顶层保存图像的更加全局,更加抽象的信息,而内容就是全局信息,所以,计算顶层的激活值(原始图像在顶层的激活值和生成图像在顶层的激活值)的差值,就可以当成是损失函数

8.3.2 风格损失(使用卷积神经网络的多个层)

使用层激活的格拉姆矩阵,即某一层特征图的内积,这个内积可以被理解成表示该层特征之间相互关系的映射.这些特征相互关系抓住了在特定空间尺度下模式的统计规律,从经验上来看,它对应于这个尺度上找到的纹理的外观

8.3.3 用keras实现神经风格迁移

1.创建一个网络,她能够同时计算风格参考图像,目标图像和生成图像的VGG19网络的层的激活

2.使用这三张图象上计算的层激活来定义之前叙述的损失函数,为了实现风格迁移,需要将这个损失函数最小化

3.设置梯度下降过程来讲这个损失函数最小化

# 定义初始变量
from keras.preprocessing.image import load_img,img_to_arraytarget_image_path = r'D:\study\Python\Deeplearning\Untitled Folder\portrait.jpg'
style_reference_image_path = r'D:\study\Python\Deeplearning\Untitled Folder\style.jpg'width,height = load_img(target_image_path).size
img_height = 400
img_width = int(width*img_height/height)
# 辅助函数
import numpy as np
from keras.applications import vgg19def preprocess_image(image_path):img = load_img(image_path,target_size = (img_height,img_width))img = img_to_array(img)img = np.expand_dims(img,axis = 0)img = vgg19.preprocess_input(img)return imgdef deprocess_image(x):x[:,:,0] += 103.939x[:,:,1] += 116.779x[:,:,2] += 123.68x = x[:,:,::-1]x = np.clip(x,0,255).astype('uint8')return x
from keras import backend as Ktarget_image = K.constant(preprocess_image(target_image_path))
style_reference_image = K.constant(preprocess_image(style_reference_image_path))
combination_image = K.placeholder((1,img_height,img_width,3))# 将三张图片合并为一个批量
input_tensor = K.concatenate([target_image,style_reference_image,combination_image],axis = 0)model = vgg19.VGG19(input_tensor = input_tensor,weights = 'imagenet',include_top = False)
print('load  model')

定义损失函数

# 内容损失
def content_loss(base,combination):return K.sum(K.suqare(combination-base))# 风格损失
def gram_matrix(x):features = K.batch_flatten(K.permute_dimensions(x,(2,0,1)))gram = K.dot(features,K.transpose(features))return gramdef style_loss(style,combination):S = gram_matrix(style)C = gram_matrix(combination)channels = 3size = img_height * img_widthreturn K.sum(K.square(S-C))/(4. * (channels**2)*(size**2))# 除了内容损失和风格损失,我们还定义了第三个损失--总变差损失,它对生成的组合图象的像素进行操作,使生成的图像具有空间连续性def total_variation_loss(x):a = K.square(x[:,:img_height - 1,:img_width - 1,:] - x[:,1:,:img_width - 1,:])b = K.square(x[:,:img_height - 1,:img_width - 1,:] - x[:,:img_height - 1,1:,:])return K.sum(K.pow(a+b,1.25))
# 定义需要最小化的最终损失
outputs_dict = dict([(layer.name,layer.output) for layer in model.layers])# 用于内容损失的层
content_layer = 'block_conv2'# 用于风格损失的层
style_layers = ['block1_conv1','block2_conv1','block3_conv1','block4_conv1','block5_conv1',]# 损失分量加权平均所使用的权重
total_variation_weight = 1e-4
style_weight = 1.
content_weight = 0.025# 添加内容损失
loss = K.variable(0.)
layer_features = outputs_dict[content_layer]
target_image_features = layer_features[0,:,:,:]
combination_features = layer_features[2,:,:,:]
loss += content_weight * content_loss(target_image_features,combination_features)# 添加每个目标层的风格损失变量
for layer_name in style_layers:layer_features = outputs_dict[layer_name]style_reference_features = layer_features[1,:,:,:]combination_features = layer_features[2,:,:,:]sl = style_loss(style_reference_features,combination_features)loss += (style_weight / len(style_layers)) * sl#  添加总变差损失
loss += total_variation_weight * total_variation_loss(combination_image)
# 使用L-BFGS方法进行优化,创建一个python类,可以同时计算损失值和梯度值,在第一次调用时会返回损失值,同时缓存梯度值用于下一次调用
grads = K.gradients(loss,combination_image)[0]fetch_loss_and_grads = K.function([combination_image],[loss,grads])class Evaluator(object):def __init__(self):self.loss_value = Noneself.grads_values = Nonedef loss(self,x):assert self.loss_values is Nonex = x.reshape((1,image_height,img_width,3))outs = fetch_loss_and_grads([x])loss_value= outs[0]grad_values = outs[1].flaten().astype('float64')self.loss_value = loss_valueself.grad_values = grad_valuesreturn self.loss_valuedef grads(self,x):assert self.loss_value is not Nonegrad_values = np.copy(self.grad_values)self.loss_value =Noneself.grad_values = Nonereturn grad_valuesevaluator = Evaluator()
# 风格迁移循环
from scipy.optimize import fmin_1_bfgs_b
from scipy.misc import imsave
import time result_prefix = 'my_result'
iterations = 20# 将初始图片展开成向量
x = preprocess_image(target_image_path)
x = x.flatten()for i in range(iterations):#     # 对生成图像的像素运行L-BFGS最优化,以将神经风格损失最小化print('Start of iterations:',i)start_time = time.time()x,min_val,info = fmin_1_bfgs_b(evaluator.loss,x,fprime = evaluator.grads,maxfun)#     保存当前生成的图片print('Current loss value:',min_val)img = x.copy().reshape((img_height,img_width,3))img = deprocess_image(img)fname = result_prefix+ '_at_iteration_%d.png' % iimsave(fname,img)print('Image saved as',fname)end_time = time.time()print('Iteration %d completed in %ds' % (i,ent_time - start_time))

小结

1.风格迁移是指创建一张新的图片,保留目标图像的内容的同时还抓住了参考图像的风格

2.内容可以被娟卷积神经网络更靠近顶层的层所捕捉到

3.风格可以被卷积神经网络不同层激活的内部相互关系所捕捉到

4.深度学习那个将风格迁移表述为一个最优化的过程,并用到了一个用预训练卷积神经网络所定义的损失

《python深度学习》学习笔记与代码实现(第八章:8.1,8.2,8.3)相关推荐

  1. python深度神经网络文本二分类代码_如何用Python和深度神经网络识别图像?

    只需要10几行Python代码,你就能自己构建机器视觉模型,对图片做出准确辨识和分类.快来试试吧! 视觉 进化的作用,让人类对图像的处理非常高效. 这里,我给你展示一张照片. 如果我这样问你: 你能否 ...

  2. Python初学者零碎基础笔记(一)

    Python初学者零碎基础笔记 一行代码输入多个参数 方法1.) a,b,c=map(类型,input("请输入").split()) #默认空格分隔,若要转其他类型,把类型换成需 ...

  3. python structure_GitHub - CYZYZG/Data_Structure_with_Python: 这是我在学习《基于Python的数据结构》的时候的笔记与代码...

    Data_Structure_with_Python 这是我在学习<基于Python的数据结构>的时候的笔记与代码 主要参考:数据结构与算法(Python) 对于算法的时间效率,我们可以用 ...

  4. 新建网站了!Github标星过万的吴恩达机器学习、深度学习课程笔记,《统计学习方法》代码实现,可以在线阅读了!...

    吴恩达机器学习.深度学习,李航老师<统计学习方法>,可以说是机器学习入门的宝典.本文推荐一个网站"机器学习初学者",把以上资源的笔记.代码实现做成了网页版,可以在线阅读 ...

  5. 《Python 深度学习》刷书笔记 Chapter 3 预测房价:回归问题

    文章目录 波士顿房价数据集 3-24 加载波士顿房价数据 3-25 数据标准化 3-26 模型定义 3-27 K折验证 3-28 训练500轮,保存每折的验证结果 3-29 计算所有轮次茨种的K折验证 ...

  6. 《Python 深度学习》刷书笔记 Chapter 8 Part-1 生成式深度学习

    文章目录 生成式深度学习 8-1 对于不同的softmax温度,对概率进行重新加权 实现字符级的LSTM文本生成 8-2 下载并解析初始文本文件 8-3 将字符序列向量化 8-4 用于预测下一个字符的 ...

  7. 《Python 深度学习》刷书笔记 Chapter 4 关于电影评论模型的进一步探讨

    文章目录 电影评论模型的进一步改进 4-3 原始模型 4-4 容量更小的模型 4-5 容量更大的模型 4-6 向模型中添加L2权重正则化 写在最后 电影评论模型的进一步改进 我们将在这一节使用实验的方 ...

  8. 《Python深度学习》第五章-5(可视化过滤器)读书笔记

    5.4 卷积神经网络的可视化 卷积神经网络学到的表示非常适合可视化,很大程度上是因为它们是视觉概念的表示\color{red}视觉概念的表示视觉概念的表示.接下来介绍3种可视化方法. 事中\color ...

  9. 《Python 深度学习》刷书笔记 Chapter 5 Part-4 卷积神经网络的可视化(Fillter)

    文章目录 可视化卷积神经网络 2-25 读入模组 5-26 观察图像 观察卷积层特征提取 5-27 建立多输出模型观察输出 5-28 显示图像 5-29 打印全部的识别图 5-32 为过滤器的可视化定 ...

最新文章

  1. PageRank行将过时 搜索引擎遭遇范式转移
  2. php curl获取响应,php – cUrl – 获取html响应正文
  3. 前端工程师必须要知道的SEO技巧(2):制作比设计还要漂亮的代码(内容和语义化代码)上...
  4. 数学猜想验证步骤_高中数学解题思路与技巧汇总,19种解题方法,实用!
  5. node那点事(二) -- Writable streams(可写流)、自定义流
  6. 腾讯,字节等大厂面试真题汇总,深夜思考
  7. [记录]---mysql数据库,decimal类型设置默认值0,不要是null
  8. 关于预编绎网站的问题[已预编译此应用程序的错误]
  9. android 默认分辨率是多少合适,Bluestacks分辨率修改多少合适
  10. java get中文乱码怎么解决_java中get请求中文乱码怎么办?
  11. 使用APP inventor来制作一个属于自己的蓝牙串口软件
  12. java实现注册的短信验证码
  13. LS-DYNA系列_Mie-Gruneisen状态方程
  14. (解决)Circular view path [index]: would dispatch back to the current handler URL [] again. Che
  15. CSS 分析倒三角|侧三角|正三角的实现
  16. flea-jersey使用之Flea RESTful接口介绍
  17. AA Master考试认证笔记
  18. Jenkins安装并配置加速器
  19. 详解js对称加密和非对称加密
  20. 雅礼集训 Day1 T1 养花

热门文章

  1. Django 实现网站注册用户邮箱验证功能
  2. 10100java压力测试_3DMark新增压力测试 你的电脑可靠?得先过这关
  3. Python音乐可视化
  4. 商用计算机使用温度,电脑一般的使用温度为?
  5. python中data是什么意思_C++中cv::Mat中的data属性对应在python中是什么
  6. 气不气!被迫无奈赚了500W,这程序员真的吊炸天...
  7. .NET 针对465加密端口 加密协议SSL(Implicit SSL)进行的邮件发送
  8. 什么email邮箱可以作为公共邮箱?
  9. 实战Go内存泄露【转】
  10. 深度学习必备三种基本绘图技能