RuntimeError: Placeholder storage has not been allocated on MPS device!解决方案

  大家好,我是爱编程的喵喵。双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中。从事机器学习以及相关的前后端开发工作。曾在阿里云、科大讯飞、CCF等比赛获得多次Top名次。现为CSDN博客专家、人工智能领域优质创作者。喜欢通过博客创作的方式对所学的知识进行总结与归纳,不仅形成深入且独到的理解,而且能够帮助新手快速入门。

  本文主要介绍了RuntimeError: Placeholder storage has not been allocated on MPS device!解决方案,希望能对使用stable diffusion的同学们有所帮助。

1. 问题描述

  今天同学在MAC电脑上运行stable diffusion时,却遇到了RuntimeError: Placeholder storage has not been allocated on MPS device!的错误提示,具体报错信息如下图所示:

请添加图片描述

  在经过了亲身的实践后,终于找到了解决问题的方案,最终将逐步的操作过程总结如下。希望能对遇到同样bug的同学们有所帮助。

在 Streamlit 中出现 "Runtime hasn't been created!" 错误通常是由于创建 TensorFlow Session 的方式不正确导致的。 Streamlit 是一个 Web 应用程序框架,用于构建交互式数据应用程序。当你在 Streamlit 应用程序中使用 TensorFlow 时,你需要确保正确地创建 TensorFlow Session。 以下是一个可以在 Streamlit 中使用 TensorFlow 的示例: ```python import streamlit as st import tensorflow as tf # 创建计算图和 TensorFlow Session graph = tf.Graph() session = tf.Session(graph=graph) # 在 TensorFlow Session 中执行计算图的操作 with graph.as_default(): # 定义计算图的操作 x = tf.placeholder(tf.float32, shape=[None, 784]) y_ = tf.placeholder(tf.float32, shape=[None, 10]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) y = tf.nn.softmax(tf.matmul(x, W) + b) # 在 Streamlit 应用程序中使用 TensorFlow Session def main(): st.title("TensorFlow Demo") st.write("This is a demo of TensorFlow running inside Streamlit.") # 加载 MNIST 数据集 mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() # 预处理数据 x_train = x_train.reshape(-1, 784) / 255.0 x_test = x_test.reshape(-1, 784) / 255.0 y_train = tf.keras.utils.to_categorical(y_train, 10) y_test = tf.keras.utils.to_categorical(y_test, 10) # 在 TensorFlow Session 中运行训练操作 with graph.as_default(): cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) session.run(tf.global_variables_initializer()) for i in range(1000): batch = next_batch(100, x_train, y_train) train_step.run(session=session, feed_dict={x: batch[0], y_: batch[1]}) # 在 TensorFlow Session 中评估模型 with graph.as_default(): correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) acc = accuracy.eval(session=session, feed_dict={x: x_test, y_: y_test}) # 显示结果 st.write("Accuracy:", acc) if __name__ == "__main__": main() ``` 在上面的示例中,我们使用 `tf.Session(graph=graph)` 创建了一个 TensorFlow Session,并将计算图 `graph` 与之关联。然后,在 `with graph.as_default():` 代码块中定义了计算图的操作,并在 TensorFlow Session 中执行了这些操作。最后,我们在 Streamlit 应用程序中使用 TensorFlow Session 运行训练操作和评估操作。 希望这个示例能够帮助你解决 "Runtime hasn't been created!" 错误。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱编程的喵喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值